0


我正在开发一个简单的序言程序。这是我的问题。

说我已经有一个事实fruit(apple).

我希望程序接受这样的输入?- input([what,is,apple]).

和输出apple is a fruit

对于输入,?-input([is,apple,a,fruit])
而不是默认的 print trueor false,我希望程序打印一些更好的短语,例如yesandno

有人可以帮我弄这个吗?

我的代码部分如下:

input(Text) :-
   phrase(sentence(S), Text), 
   perform(S).
   %...
sentence(query(Q))     --> query(Q).

query(Query) --> 
   ['is', Thing, 'a', Category],
   { Query =.. [Category, Thing]}.

% here it will print true/false, is there a way in prolog to have it print yes/no, 
%like in other language: if(q){write("yes")}else{write("no")}
perform(query(Q))     :- Q.   
4

1 回答 1

0

在 Prolog 中有一个构造 if/else:

perform(query(Q)) :-
  (  Q
  -> write(yes:Q)
  ;  write(no)
  ), nl.

当我需要对输出格式进行更严格的控制时,我使用format。不是很友好,但提供了大多数常见的选择......

于 2012-10-14T21:36:17.870 回答