1

In my prolog database, I have below facts:

played('Sharon rose', piano).
played('Robert Kay', piano).
played('Kelvin Cage', drums).
singer('Robert Kay').
band_leader('Sharon rose').

I want to print out all the names uniquely as a single list.

Below is my query using setof and the output:

setof(X, (played(X,_);singer(X);band_leader(X)), Output).

Output = ['Robert Kay','Sharon rose'] ? ;

Output = ['Kelvin Cage'] ? ;

Output = ['Robert Kay','Sharon rose']

yes

However, the output isn't what I want. I want it to print out the names uniquely as a list.

false
  • 10,264
  • 13
  • 101
  • 209
niyidrums
  • 13
  • 3

1 Answers1

1

You get multiple answers due to the anonymous variable in the goal argument of setof/3. Try instead:

?- setof(X, Y^(played(X,Y);singer(X);band_leader(X)), Output).
Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • 1
    I missed the concept of the anonymous variable in respect to the argument setof/3. Thanks, it's okay now. – niyidrums Feb 07 '21 at 02:44