Reputation: 1490
im a beginner on prolog, and I am trying to return a list of results.
Say i have items belonging to a person ie.
items(person1,apple).
items(person1,orange).
I want to be able to create a function that can return the list of items belonging to that person. At the moment I have:
getitems(Person,Result):-items(Person,N),Result is N.
This only returns the first item. How can i get it to return a list of all the items that belong to the person?
Thanks.
Upvotes: 0
Views: 55
Reputation:
Asked plenty of times, the correct nomenclature is "Finding all solution to a goal", for example, from the SWI-Prolog implementation, finding all solutions.
In short,
bagof(Item, items(person1, Item), Items).
The predicates in this section, findall
, bagof
, and setof
all behave slightly differently and have their uses. There is plenty of examples on Stackoverflow on how to use each of them.
Upvotes: 1