Prolog 如何编写好谓词(不在两者之间)
我写代码如下Prolog 如何编写好谓词(不在两者之间),prolog,clpfd,Prolog,Clpfd,我写代码如下 testb :- X::1..10, V1 = 3, V2 = 6, testbb(X,V1,V2), writeln(X). testbb(X,V1,V2) :- ( count(I,V1,V2),param(X,V1,V2) do X#\=I ). ?- testb. Yes (0.00s cpu) _385{[1, 2, 7 .. 10]} 它工作得很好,但我认为它效率不高 非常感谢:
testb :-
X::1..10,
V1 = 3,
V2 = 6,
testbb(X,V1,V2),
writeln(X).
testbb(X,V1,V2) :-
(
count(I,V1,V2),param(X,V1,V2) do
X#\=I
).
?- testb.
Yes (0.00s cpu)
_385{[1, 2, 7 .. 10]}
它工作得很好,但我认为它效率不高
非常感谢:)您可以通过以下方式将
X
域限制在V1
到V2
范围之外:
not_between(X, Lower, Upper) :-
% it is not the case that X is both greater and
% equal to Lower, and less than or equal to Upper:
#\ ((X #>= Lower) #/\ (X #=< Upper)).
在这个小例子中,它太快了,无法测量,所以你怎么知道它是无效的呢?此外,这是哪种序言方言?ECLiPSe?是的,ECLiPSe clp,我是用逻辑OOP写这段代码的,我不太理解prolog的逻辑,所以,我无法确认:(你使用哪一个prolog?它在ECLiPSe clp和SWI prolog中不起作用,谢谢:)这是使用clp(FD)与SWI prolog一起测试的。我在1..10中使用了
X,而不是(X,3,6)
来测试谓词,并且X的域确实被划分为[1,2]
和[7,…,10]
定义的集合。
:- use_module(library(clpfd)).