For Programmers: Free Programming Magazines  


Home > Archive > Prolog > September 2004 > Newbie-Question









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 Newbie-Question
Luigi Motorra

2004-09-06, 4:02 pm

Hi, I'm a very beginner in prolog - so please be patient

human(man,john).
human(man,ben).

same_sex(person1,person2) :- human(X,person1) ==
human(X,person2).

This will cause a no for
same_sex(john,ben).

What is wrong and what should I learn/read about?

thanx
Luigi Motorra
Luigi Motorra

2004-09-06, 4:02 pm

Sorry about the simple failure with big and small letters - thats not my
problem. I meant

human(man,john).

human(man,ben).

same_sex(Person1,Person2) :- human(X,Person1) == human(X,Person2).

Luigi

Stefan Nobis

2004-09-06, 4:02 pm

"Luigi Motorra" <Luigi_MotorraDELETETHIS@gmx.net> writes:

> same_sex(Person1,Person2) :- human(X,Person1) == human(X,Person2).


You don't want to compare facts but to fulfill two facts at the
same time -- that's a logical and. Try this:

same_sex(Person1,Person2) :-
human(X,Person1),
human(X,Person2).

--
Stefan.
Luigi Motorra

2004-09-07, 9:07 am

thanx - that works fine - and now I'm going to try to understand why ;-)

Luigi

Luigi Motorra

2004-09-07, 9:07 am

How do i make another question

different_sex(Person1,Person2) :- ??????

Luigi
Stefan Nobis

2004-09-07, 9:07 am

"Luigi Motorra" <Luigi_MotorraDELETETHIS@gmx.net> writes:

> How do i make another question


> different_sex(Person1,Person2) :- ??????


I'm no expert, so i don't know if this is a good solution, but it
works:

different_sex(Person1, Person2) :-
human(X, Person1),
human(Y, Person2),
X \== Y.

Prolog tries to unify the variables and then tests, if X and Y are
not both bound to the same value.

You can read it like this:

If there is a person with sex X and another person (maybe quite
the same as the first) with sex Y and X and Y are different, then
the predicate different_sex evaluates to true.

--
Stefan.
Luigi Motorra

2004-09-07, 4:10 pm

Thanx a lot - now I'm going to understand - but on last question:

What is the diffrence between \== and \= ?

Luigi
Stefan Nobis

2004-09-08, 9:01 am

"Luigi Motorra" <Luigi_MotorraDELETETHIS@gmx.net> writes:

> Thanx a lot - now I'm going to understand - but on last question:


> What is the diffrence between \== and \= ?


IIRC = or \= tries to bind variables (-> unifying), but == or \==
does not (needed e.g. for arithmetic). So in the last example \=
would have worked, too.

--
Stefan.
russell kym horsell

2004-09-09, 3:56 am

Stefan Nobis <stefan@snobis.de> wrote:
> "Luigi Motorra" <Luigi_MotorraDELETETHIS@gmx.net> writes:
[color=darkred]
> IIRC = or \= tries to bind variables (-> unifying), but == or \==
> does not (needed e.g. for arithmetic). So in the last example \=
> would have worked, too.


\= should not "end up" binding anything, of course. ;)

== and \== simply test for equality (in-) of general terms "literally".

=:= and =\= test numberical values for equality (in-) after
arguments are interpreted as arith expressions.

So given X and Y and unbound variables

X = Y because all unbound variables are "equal"
(as a side-effect X and Y will not refer to the same value
for the rest -- "to the right" in the current clause).

X == Y fails because X and Y are not literally the same unbound variable
(no side effects -- X and Y can still eb bound differently
in the remainder of the clause).

X \= Y fails because X and Y could be bound to the same value
(no S.E.).

X \== Y succeeds because X and Y are not literally the same
(no S.E.).

X =:= Y gets an error because nothing is instantiated.

X =\= Y gets an error because nothing is instantiated.

--
kym@kym.massbus.org
Sponsored Links







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

Copyright 2008 codecomments.com