Reputation: 21
I want to write a function to write a list that, for example, for the code below
likes('john', 'soccer')
likes('mary', 'football')
likes('eric', 'soccer')
So the function I want to write would be like
whoLikes('soccer', list)
And the list would be ('john', 'eric')
Should I use recursion to do this? How?
Upvotes: 1
Views: 44
Reputation: 20669
Use can use the built-in predicate setof/3
:
likes(messi,soccer).
likes(ronaldo,soccer).
likes(jordan,basketball).
whoLikesSoccer(F):- setof(X,likes(X,soccer),F).
OUTPUT
?- whoLikesSoccer(X).
X=[messi,ronaldo].
false
Upvotes: 1