prologprolog-setof

Prolog setOf with Multiple Predicates


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.


Solution

  • 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).