For Programmers: Free Programming Magazines  


Home > Archive > Prolog > December 2004 > Re: Duplicate results after queries









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Duplicate results after queries
Pere Montolio

2004-12-07, 4:13 am

"Swartz" <swartz@inbox.ru> wrote in message news:<10r0bs9tccfri71@corp.supernews.com>...
> 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.


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).
Markus Triska

2004-12-07, 4:13 am

Pere 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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com