bagof(Term,Goal,List):
This is the same as findall(Term,Goal,List), except for its treatment of free variables that occur in Goal but do not occur in Term. It first picks the first tuple of values for the free variables, and then uses this tuple to find the list of solutions List of Goal. It enumerates all of 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):
This is like bagof(Term,Goal,List), except that the elements of List are sorted into alphabetical order.