0

下面的代码应该输出:

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.

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

描述:一个农民带着他的山羊、狼和卷心菜来到一条他们想渡过的河边。有一条船,但只能坐两个人,只有农夫会划船。如果山羊和卷心菜同时上船,卷心菜就会被吃掉。同样,如果没有农夫,狼和山羊在一起,山羊就会被吃掉。设计一系列的河流过境点,以便所有相关人员安全过河。

4

1 回答 1

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]).

可能还有其他几种方法...... :)

于 2015-03-10T23:42:22.663 回答