1

I have simplified a more complex problem to the following: there are three houses on a street with three different colours (no colour repeated); red, blue, green. Write a program using DCGs to simulate all permutations/possibilities. My code won't run and I'm struggling to see why. Any corrections would really help.

s --> h(X), h(Y), h(Z), X\=Y, X\=Z, Y\=Z.

h(X) --> Col(X).

Col(X) --> [red].
Col(X) --> [blue].
Col(X) --> [green].
4

2 回答 2

2

s/Col/col/

然后,您在s//0Prolog 目标中使用代替非终端。那行不通,您需要{}//0像这样“逃脱”它们

s -->h(X),h(Y),h(Z),{X\=Y,X\=Z,Y\=Z}.

但我宁愿写:

s --> {dif(X,Y), dif(Y,Z), dif(X,Z)}, h(X),h(Y),h(Z).

Prolog 以这种方式为您执行所有簿记。

如果我们在它。不要忘记通过调用非终端phrase/2。因此:

?- phrase(s, L).
于 2014-12-01T20:47:05.470 回答
2

您(也)忘记从叶子中“返回”值:

...
col(red)-->[red].
...

对于如此小的数据集,很容易对排列进行硬编码:

s --> r,g,b ; r,b,g ; g,r,b ; b,r,g ; g,b,r ; b,g,r.
r --> [red].
g --> [green].
b --> [blue].
于 2014-12-01T23:01:29.440 回答