panza
panza

Reputation: 1431

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 without using the semicolon every time.

Thanks,

I.

Upvotes: 3

Views: 2412

Answers (1)

Roland Illig
Roland Illig

Reputation: 41617

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.

Upvotes: 1

Related Questions