A solution is:
The farmer takes the Goat from west of the river to east
The farmer crosses the river from east to west
The farmer takes the cabbage from west of the river to east
The farmer takes the Goat from east of the river to west
The farmer takes the Wolf from west of the river to east
The farmer crosses the river from east to west
The farmer takes the Goat from west of the river to east
No
?- solve.
A solution is:
ERROR: write_move/2: Undefined procedure: write/4
ERROR: However, there are definitions for:
ERROR: write/1
ERROR: writeq/1
ERROR: write/2
ERROR: writeq/2
Exception: (10) write('The farmer takes the Goat from ', west, ' of the river to ', east) ?
write_move(state(X,W,G,C),state(Y,W,G,C)):- !,
write('The farmer crosses the river from ',X,' to ',Y), nl.
write_move(state(X,X,G,C),state(Y,Y,G,C)):- !,
write('The farmer takes the Wolf from ',X,' of the river to ',Y), nl.
write_move(state(X,W,X,C),state(Y,W,Y,C)):- !,
write('The farmer takes the Goat from ',X,' of the river to ',Y),nl.
write_move(state(X,W,G,X),state(Y,W,G,Y)):- !,
write('The farmer takes the cabbage from ',X,' of the river to ',Y),nl.
如果有人有兴趣,此代码的提示是:
描述:一位农民带着他的山羊,狼和卷心菜来到他们希望穿过的河里。有一条船,但它只有两个人的空间,农民是唯一可以划船的人。如果山羊和卷心菜同时进入船上,卷心菜就会被吃掉。同样地,如果狼和山羊没有农民在一起,山羊就会被吃掉。设计河流的一系列交叉点,以便所有相关人员安全地穿越河流。
答案 0 :(得分:1)
write/1
只接受一个参数。 未定义的过程write/4
表示没有定义write/4
(write
接受4个参数)。该错误消息表明write/1
存在。
作为替代:
write('The farmer takes the Wolf from ',X,' of the river to ',Y), nl.
您可以执行以下操作之一:
write('The farmer takes the Wolf from '),
write(X),
write(' of the river to '),
write(Y), nl.
或者...
format('The farmer takes the Wolf from ~w to ~w~n', [X, Y]).
或(在SWI Prolog中)......
writef('The farmer crosses the river from %w to %w\n', [X, Y]).
可能还有其他两种方式...... :)