Prolog:用空格读取输入会导致错误

时间:2010-11-04 19:39:43

标签: prolog

这是我的程序

tran('father','otosan').
tran('father','chichiwe').
tran('mother','okasan').
tran('mother','hahawe').
tran('hi','ohayo').
tran('good night','oyasemi').
tran('good bye','sayonara').
tran('do your best','gambaru').
tran('please','onegai').
tran('sorry','gomen').
tran('thank you','aregatto').
tran('cute','kawaii').
eng:- nl,write('enter a word in english: '),read(X),jap(X).
jap(X):- tran(X,Y),write('the word in japanese is '),write(Y),nl,fail.
jap(Z).
:-eng.

我在带空格的单词中出错了

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

看来,当输入带空格的单词时,您需要用单引号括起来:

?- ['trans.pl'].
% trans.pl compiled 0.00 sec, 5,432 bytes
true.

?- eng.

enter a word in english: hi.
the word in japanese is ohayo
false.

?- eng.

enter a word in english: 'good bye'.
the word in japanese is sayonara
false.

发生这种情况的原因是,当您输入hi时,Prolog会将其统一到jap(hi).并解析为ohayo。当您输入good bye时,Prolog会将其统一到jap(good bye),这会给您一个错误(操作员预期)。这就是您需要将输入引用为'good bye'的原因。然后Prolog会将其统一到jap('good bye'),它会为您提供所需的内容。