我正在尝试在Prolog问题中打印所有事实。已经搜索了一段时间的论坛,似乎无法找到问题的解决方案。我尝试了how can I print all database facts in prolog,但似乎无法使其发挥作用。我正在为它做一个菜单,所以,当我按下相应的键时,它应该显示所有事实并打印回菜单。像这样的东西(它不完整):
if_then_else(P,Q,R):- P,!,Q.
if_then_else(P,Q,R):- R.
:- dynamic client/2.
client(john,password).
client(charles,bird).
printmenu:- write('1-Print all facts').
read(X),
if_then_else(X=1,printfacts,(error,printmenu)).
printfacts:-
答案 0 :(得分:3)
为什么不
printfacts :-
forall(client(X, Y),format('client(~w, ~w)~n', [X, Y])).
答案 1 :(得分:1)
如果您只想打印事实(不是将它们全部列在列表或其他内容中),那么:
printfacts :-
client(X, Y),
format('client(~w, ~w)~n', [X, Y]),
false.
printfacts.
您的printmenu
可写:
printmenu :-
repeat,
write('1-Print all facts').
read(X),
( X = 1
-> printfacts
; write('Invalid response'), nl,
),
fail. % Go back to the top and reprompt