Fernando Remde
Fernando Remde

Reputation: 21

Creating a specific list in Prolog

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

Answers (1)

Ch3steR
Ch3steR

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

Related Questions