creates a list with all solutions of a goal for all instantiations of the free variables in that goal.
bagof/3 unifies TermList for each different instantiation of the free variables in Goal with a list of all instantiations of Term where Goal is a solution of Term. Each variable, wich occurs in Goal and does not occur in Term or Template is called a free variable.
Term term Template term Goal goal TermList list
part_of(house, window). part_of(house, door). part_of(house, room). part_of(room, table). part_of(room, floor). part_of(floor, tile).contains(X, Y) :- part_of(X, Y). contains(X, Y) :- part_of(X, Z), contains(Z, Y).
?- bagof(Element, contains(What, Element), List).
Element = _1 What = house List = [window,door,room,table,floor,tile];
Element = _1 What = room List = [table,floor,tile];
Element = _1 What = floor List = [tile]
?- bagof(Element, What ^ contains(What, Element), List).
Element = _1 What = _2 List = [window,door,room,table,floor,tile,table,floor,tile,tile]
?- bagof(Element, contains(house, truck), List)
no
This predicate is part of the ISO-Prolog Standard.
| scroll to top |
|