2

抱歉,如果这很明显,但我最近一直在学习 prolog,并试图读入数据以在推荐系统中使用。

 gifter :- write('how much money?  '), read(money), nl,
          assert(will_spend(money)),
          write('Is the giftee classy?  '), read(classy), nl.

前面的代码应该读入用户希望花费的金额,然后询问受赠人的个性,然而,只询问第一个问题。它似乎进入了新行,但没有断言谓词:

?- will_spend(30)。[警告:未定义谓词:will_spend/1']

这是为什么,我做错了什么?在此先感谢您的帮助。

4

1 回答 1

1
gifter :- write('how much money?  '), read(Money), nl,
          assert(will_spend(Money)),
          write('Is the giftee classy?  '), read(Classy), nl,
          assert(classy :- Classy = 'yes').

然后,

?- gifter.
how much money?  127.

Is the giftee classy?  yes.

true.

?- classy.
true.

?- will_spend(X).
X = 127.

请记住,需要read一个句号和一个换行符;此外,变量应该大写。

于 2015-10-21T03:49:57.107 回答