简体   繁体   中英

print customized result in prolog


I am working on a simple prolog program. Here is my problem.

Say I already have a fact fruit(apple).

I want the program take a input like this ?- input([what,is,apple]).

and output apple is a fruit

and for input like ?-input([is,apple,a,fruit])
instead of default print true or false , I want the program print some better phrase like yes and no

Can someone help me with this?

My code part is below:

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.   

In Prolog there is a construct if/else:

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

When I need a stricter control on output formatting, I use format . Not very friendly, but offers most of the usual options...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM