bagof(Term,Goal,List):
The same as findall(Term,Goal,List) except for its treatment of free
variables that occur in Goal but not in Term.
It will first pick the first tuple of values for the free variables and
then use this tuple to find the list of solutions List of Goal. It enumerates all the tuples for the free variables. Example:
?-bagof(Y,member((X,Y),[(1,a),(2,b),(3,c)]),Xs)
X=1
Y=[a];
X=2
Y=[b];
X=3
Y=[c];
no
setof(Term,Goal,List):
Like bagof(Term,Goal,List) but the elements of List are
sorted into alphabetical order.