Prolog的写功能问题

时间:2015-03-10 23:23:40

标签: prolog

以下代码应输出:

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.

如果有人有兴趣,此代码的提示是:

描述:一位农民带着他的山羊,狼和卷心菜来到他们希望穿过的河里。有一条船,但它只有两个人的空间,农民是唯一可以划船的人。如果山羊和卷心菜同时进入船上,卷心菜就会被吃掉。同样地,如果狼和山羊没有农民在一起,山羊就会被吃掉。设计河流的一系列交叉点,以便所有相关人员安全地穿越河流。

1 个答案:

答案 0 :(得分:1)

write/1只接受一个参数。 未定义的过程write/4 表示没有定义write/4write接受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]).

可能还有其他两种方式...... :)