0

Please forgive me if this is a dumb question, but I'm a rank beginner at Prolog (been about a couple hours so far!) and am a bit confused. Not sure if I'm trying to do something beyond my level.

Imagine I have a set of people...

parent(daddy, john).
parent(daddy, jim).

...and the following predicate to find siblings...

sib(X, Y) :- parent(Z, X), parent(Z, Y), not(X = Y).

If I run sib(X, Y)., it works fine, but gives me duplicates, eg...

X = jim,
Y = john ;
X = john,
Y = jim ;
false

I'm trying to work out how to avoid the duplicates.

I saw this answer, which uses setof, and spent some time reading about that predicate, but a) can't work out if it's what I need, and b) can't (yet) grok it.

Anyone able to help a newbie? Thanks

In case it makes any difference, I'm using SWI-Prolog on Ubuntu

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • 1
    To "break symmetry" you can use e.g. `Var1 @> Var2`. Also, you are using pretty much the worst possible variable names. E.g. P (parent) and C (child) are far easier to read. – brebs Mar 01 '23 at 23:39
  • @brebs Thanks for the comment. Please can you explain how I'd use `@>` as I can't find a clear explanation of what it is. I know what you mean about the variable names, I was copying from a book. Since posting, I got myself confused enough to use more sensible names! – Avrohom Yisroel Mar 01 '23 at 23:41
  • @brebs Ah, right after posting I found it! I just added `, X @> Y` as a clause and it worked a treat. Thanks – Avrohom Yisroel Mar 01 '23 at 23:45

0 Answers0