快到了……其实代码已经有了,我们来使用吧!
:- use_module(library(clpfd)).
运行我们得到dd//1
:phrase/2
?- C #>= 3, phrase(dd(C), Xs).
C = 3, Xs = [0,0,0]
; C = 4, Xs = [0,0,0,0]
; C = 5, Xs = [0,0,0,0,0]
; C = 6, Xs = [0,0,0,0,0,0]
; C = 7, Xs = [0,0,0,0,0,0,0]
; C = 8, Xs = [0,0,0,0,0,0,0,0]
; C = 9, Xs = [0,0,0,0,0,0,0,0,0]
...
序列包含在哪里1
?我们知道它们必须存在......
?- Xs = [0,0,0,1,0], C #>= 3, 短语(dd(C), Xs)。
Xs = [0,0,0,1,0],C = 3
; 错误的。
...但它们没有出现在上述答案序列中:
?- C#>= 3, 短语(dd(C), Xs), Xs = [0,0,0,1,0]。
**循环**
为了强制对解决方案集进行公平枚举,我们可以使用length/2
如下目标:
?- C#>= 3,长度(Xs,_),短语(dd(C),Xs)。
C = 3,Xs = [0,0,0]
; C = 4,Xs = [0,0,0,0]
; C = 5,Xs = [0,0,0,0,0]
; C = 3,Xs = [0,0,0,0,1]
; C = 3,Xs = [0,0,0,1,0]
; C = 3,Xs = [0,0,1,0,0]
; C = 3,Xs = [0,1,0,0,0]
; C = 3,Xs = [1,0,0,0,0]
; C = 6,Xs = [0,0,0,0,0,0]
...