sitelogo
bagof/3

creates a list with all solutions of a goal for all instantiations of the free variables in that goal.

bagof(+Term, +(Template ^ Goal), ?TermList)
bagof(+Term, +Goal, ?TermList)

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.

Arguments

Term                   term
Template               term
Goal                   goal
TermList               list

Examples


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

Standard

This predicate is part of the ISO-Prolog Standard.

See also

findall/3, setof/3


Up read on...
scroll to top managed with ubiCMS