Variable binding in Prolog
I have written a predicate common_participant(Person, PairEvent)
. which returns pairs of facts from my knowledge base. I was wondering whether there is any way to perform variable binding and collect all results wi开发者_开发百科thout using the semicolon every time.
Thanks,
I.
Yes, you can use findall/3
. But depending on what you really want to do, there are often better ways. Do you want to output things? Then try this:
print_participants :-
common_participant(Person, PairEvent),
write(Person), write(' participates in '), write(PairEvent), write('.'), nl,
fail.
print_participants :-
true.
That way, you don't need to keep all combinations in a large list at the same time, but only the one that is needed for printing.
Edit: Fixed the code, as suggested by Kaarel.
精彩评论