作者:L-壹_413 | 来源:互联网 | 2023-01-13 21:00
有没有办法约束列表中的元素是否在范围内,例如1-6而不是使用between(1,6,X)
?
1> Willem Van O..:
该clpfd
库有一个ins/2
谓词,您可以在其中指定变量列表 - 元素的范围.
喜欢:
all_between(Low, High, Vars) :-
Vars ins Low..High.
打电话时all_between(1,6,[X1,X2,X3]).
我们得到:
X1 in 1..6,
X2 in 1..6,
X3 in 1..6.
如果我们想要枚举元素,我们也可以使用label(L).
它来分配值:
all_between_enumerate(Low, High, Vars) :-
Vars ins Low..High,
label(Vars).
为此all_between_enumerate(1,2,[X1,X2,X3]).
生成:
?- all_between_enumerate(1,2,[X1,X2,X3]).
X1 = X2, X2 = X3, X3 = 1 ;
X1 = X2, X2 = 1,
X3 = 2 ;
X1 = X3, X3 = 1,
X2 = 2 ;
X1 = 1,
X2 = X3, X3 = 2 ;
X1 = 2,
X2 = X3, X3 = 1 ;
X1 = X3, X3 = 2,
X2 = 1 ;
X1 = X2, X2 = 2,
X3 = 1 ;
X1 = X2, X2 = X3, X3 = 2.
如果您已经为变量添加了约束,例如X1 #,这些也被考虑在内:
?- X1 #