Prolog: Reading input with spaces causes an error
this is my program
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('sorr开发者_Go百科y','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.
I got an error in the words with spaces
how can I solve this problem?
It appears that when inputting words with spaces, you need to surround them with single quotes:
?- ['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.
The reason this is happening is that when you enter hi
, Prolog is unifying it to jap(hi).
which resolves to ohayo
. When you enter good bye
, Prolog unifies it to jap(good bye)
, which will give you an error (Operator Expected). This is why you need to quote your input as 'good bye'
. Prolog will then unify it to jap('good bye')
, which gives you what you want.
精彩评论