PPT Slide
Gathering Answers into Bags or Sets
The predicates bagof and setof are used to gather instances of objects.
We specify a goal, a variable in the goal, and a bag or set name.
For each success of the goal, the constant that matched this variable is gathered into the bag or set.
|?- bagof( Child, parent( jan, Child ), B ) .
|?- bagof( Child, Who^ ( parent ( Who, Child ) ), B ) .
B = [ bet, cat, ann, cat ]
|?- setof( Child, P^( parent ( P, Child ) ), S ) .