开发者

Variable binding in Prolog

开发者 https://www.devze.com 2023-01-08 04:59 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消