[newbie] siblings and surnames
From: Fabio (dont_at_spam.please)
Date: 07/21/04
- Next message: Nameless: "Re: matrix"
- Previous message: Tommy: "matrix"
- Next in thread: Nameless: "Re: [newbie] siblings and surnames"
- Reply: Nameless: "Re: [newbie] siblings and surnames"
- Reply: Cesar Rabak: "Re: [newbie] siblings and surnames"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 21 Jul 2004 21:47:40 GMT
Hello,
I'm a prolog newbie, studying it for my IA course, and trying to
implement some things I need for an exam...
I've read trough the archives, and I saw that the commutativity/simetry
topic was already covered, and that's been of great help to me, however
i'm now unable to solve another problem.
My problem doesn't concern siblings and surnames, but to not introduce
even more confusion in the discussion I thought I'd simply reuse the
terms used in the past thread about simmetry rules.
Let's assume that we already have this simmetric predicate called
siblings/2, which basically states that
siblings(X,Y) <==> siblings(Y,X)
Now, I want to code the rule which says that "All siblings share the
same surname". Question is: how the heck do I do that without incurring
in a loop?
What I've come up with is this:
%all siblings share the same surname
surname(X, Y) :- siblings(X, Z), surname(Z, Y).
%john's surname is smith.
surname(john, smith).
%john and paul are sibligs
siblings(john, paul).
But of course the first clause causes a loop, if I type
surname(paul, X).
If I put the first clause as last, that is this way:
%john's surname is smith.
surname(john, smith).
%john and paul are sibligs
siblings(john, paul).
%all siblings share the same surname
surname(X, Y) :- siblings(X, Z), surname(Z, Y).
then, for how prolog does the search, it works, somwhat, because when it
encounters the surname predicate in the last clause's body it starts its
search from the first clause in the program and thus fids surname(john,
smith). However, if I press ';' when it gives me the answer then I can
see that it went into another loop. I can fix this if I modify the last
clause this way:
%all siblings share the same surname
surname(X, Y) :- siblings(X, Z), surname(Z, Y), !.
However, 1) I don't want to put such a clause after all the facts about
the siblings relationship, and 2) this still doesn't allow me to get all
siblings with a given surname.
Any suggestions?
Fabio
- Next message: Nameless: "Re: matrix"
- Previous message: Tommy: "matrix"
- Next in thread: Nameless: "Re: [newbie] siblings and surnames"
- Reply: Nameless: "Re: [newbie] siblings and surnames"
- Reply: Cesar Rabak: "Re: [newbie] siblings and surnames"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|