Next: Removing duplicate items with
Up: Obtaining More Solutions
Previous: findall
Consider the following database :
age(peter,7).
age(anne,5).
age(pat,8).
age(tom,5).
Q: bagof(Child, age(Child,5), List)
A: List = [anne,tom]
- The bagof primitive is used like findall to
obtain multiple solutions (and has the same arguments as
findall)
- In the above example if the age is also left unspecified,
then we obtain lists according to the ages involved
Q: bagof(Child, age(Child,Age), List)
A: Age = 7
List = [peter];
Age = 5
List = [anne, tom];
Age = 8
List = [pat];
Q: bagof(Child, Age ^ age(Child,Age), List)
A: List = [peter,anne,pat,tom]
where ^ : is a predefined infix operator
- Essentially saying that we do not care about the value of age, as long
as a value exists
- bagof fails if goal to satisfy fails - in this case this is
age(Child,Age)
Omer F Rana
Thu Feb 20 20:05:17 GMT 1997