Prolog - generating numbers fitting given range

浪子不回头ぞ 提交于 2019-11-29 19:15:30

问题


I'd like to use predicates like:

range(X,0,5)
range(X,4,200)
range(X,-1000000,1000000)
dom_range(X,-1000000,1000000)

with meaning :

range(X,0,5) :- member(X,[0,1,2,3,4,5]).
range(X,4,200) :- member(X,[4,5,6...198,199,200]).
range(X,-1000000,1000000) :- member(X,[-1000000,...,1000000]).
dom_range(X,-1000000,1000000) :- domain(X, [-1000000,...,1000000]).

How to code it in Prolog nicely (taking solution performance into account - depth of recursion etc) ?

Solution is expected to run on GNU-Prolog.

P.S. Question inspired with this question.


回答1:


SWI-Prolog has the predicate between/3. so you would call it like between(0,5,X) to get the results you showed above. This predicate looks like this is implemented in C though.

If we have to write it in pure prolog (and speed&space is not a factor), you could try this following.

range(Low, Low, High).
range(Out,Low,High) :- NewLow is Low+1, range(Out, NewLow, High).



回答2:


Dave's answer is almost perfect: there is no check to see if low < high. I added a condition and now it works fine (otherwise it generates numbers from low to infinity):

range(Low, Low, High).
range(Out,Low,High) :- NewLow is Low+1, NewLow =< High, range(Out, NewLow, High).

Hope that helps!




回答3:


range in Gnu-Prolog can be solved with finite domains

range(X,Low,High) :- fd_domain(X,Low,High).

I don't know if dom_range(X,L,H) :- fd_domain(X,L,H) .

P.S. When playing with finite domains, you might like to use fd_set_vector_max/1



来源:https://stackoverflow.com/questions/7175258/prolog-generating-numbers-fitting-given-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!