Home > Archive > Prolog > February 2007 > What's wrong with this?
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 |
What's wrong with this?
|
|
| Kobayashi 2007-02-22, 4:15 am |
| Hi,
I have written a little prog. It tries to find brothers:
father(john,paul).
father(john,simon).
brother(X,Y) :- father(Z,X),father(Z,Y).
But when I ask "Who is the brother of Paul?"
brother(paul,X)
I get the answers "Paul" and "Simon". Prolog says me that Simon is
brother of Paul and additionally Paul is brother of himself.
When I ask "Who is brother of who?" I get a double-weird answer:
brother(X,Y).
Paul-Paul
Paul-Simon
Simon-Simon
Simon-Paul
What's happening? What's wrong is here and how can I avoid this?
Strange thing is this Prolog...
| |
| Carlo Capelli 2007-02-22, 4:15 am |
|
"Kobayashi" <kubilayisik@gmail.com> ha scritto nel messaggio
news:1172130609.483934.65050@q2g2000cwa.googlegroups.com...
> Hi,
>
> I have written a little prog. It tries to find brothers:
>
> father(john,paul).
> father(john,simon).
>
> brother(X,Y) :- father(Z,X),father(Z,Y).
>
> But when I ask "Who is the brother of Paul?"
>
> brother(paul,X)
>
> I get the answers "Paul" and "Simon". Prolog says me that Simon is
> brother of Paul and additionally Paul is brother of himself.
>
> When I ask "Who is brother of who?" I get a double-weird answer:
>
> brother(X,Y).
>
> Paul-Paul
> Paul-Simon
> Simon-Simon
> Simon-Paul
>
> What's happening? What's wrong is here and how can I avoid this?
>
> Strange thing is this Prolog...
>
Variables X,Y in
brother(X,Y) :- father(Z,X),father(Z,Y).
get all instantiations from each father(A,B) instance. At least you need to
tell Prolog they are distincts:
brother(X,Y) :- father(Z,X), father(Z,Y), X\=Y.
yield
?- findall(X-Y,brother(X,Y),L).
L = [paul-simon, simon-paul]
Bye Carlo
| |
| Kobayashi 2007-02-22, 8:07 am |
|
Oh, thank you very much. I could never think of the " X\=Y ". I have
appreciated this. Prolog's variable binding concept makes my head
spinned. <where am I?>
BTW, how did you learn Prolog? Could you recommend me some sources?
Master, enlighten me... :-D
| |
| Carlo Capelli 2007-02-22, 7:07 pm |
|
"Kobayashi" <kubilayisik@gmail.com> ha scritto nel messaggio
news:1172150297.170362.27590@k78g2000cwa.googlegroups.com...
>
> Oh, thank you very much. I could never think of the " X\=Y ". I have
> appreciated this. Prolog's variable binding concept makes my head
> spinned. <where am I?>
>
> BTW, how did you learn Prolog? Could you recommend me some sources?
> Master, enlighten me... :-D
>
Prolog inhabits a strange country...
In fact, I learned the basics of the language using Clocksin,Mellish book
(Programming in Prolog, if i remember well)
almost 15 years ago, writing my first C++ program, a simple Prolog
interpreter.
I like the essentiality and simplicity of the language, but I admit it's
sometime very hard to accomplish tasks that other
less esotheric languages could attack, maybe using more code.
So i never convinced my collegues to switch from VB... :(
Currently practical Prolog has evolved toward CLP, and I think you should
approach learning this subject.
Start from swi-prolog, a friendly and feature rich environment you will find
at www.swi-prolog.org
Bye Carlo
|
|
|
|
|