Code Comments
Programming Forum and web based access to our favorite programming groups."Swartz" <swartz@inbox.ru> wrote in message news:<10r0bs9tccfri71@corp.supernews.com>...[co lor=darkred] > Hi all. > > Writing a simple example: > > % parent(X,Y) => X is parent of Y > parent(bob,billy). > parent(pam,billy). > parent(bob,mandy). > parent(pam,mandy). > sibling(X,Y) :- parent(Z,X), parent(Z,Y), X \== Y. > > Any queries like the one below end up with duplicate answers. > > Ex: > | ?- sibling(X,billy). > X = mandy ? > X = mandy > no > > I understand why it happens but do not know prolog well enough to prevent > it? Any suggestions? > > Thanks in advance.[/color] Hi, "cut" is the standard way to skip overload of solutions. By example: acommonparent(LX,LY) :- member(X,LX), member(Y,LY), !. sibling(X,Y) :- setof(A,parent(A,X),LX), setof(B,parent(B,Y),LY), acommonparent(LX,LY). (you can play around this concept to adapt it to your requirement).
Post Follow-up to this messagePere Montolio wrote: > > acommonparent(LX,LY) :- > member(X,LX), > member(Y,LY), !. I think you mean: acommonparent(LX,LY) :- member(X,LX), member(X,LY), % watch out! !. The advantage of the solution I posted was its indepedence of the definition of sibling/2. If you for example change sibling/2 so that two entities are siblings of they share *both* their parents instead of only one, you can still use my solution, whereas yours would require more complicated modifications because you have to think in terms of sets instead of single entities. Best regards, Markus.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.