Prolog Question: Just started learning prolog and this was on one of the practice quizzes we were given.
Given:
avenger(thor).
avenger(captainAmerica).
sibling(thor,loki).
asgardian(thor).
asgardian(X) :- sibling(Y,X),asgardian(Y).
train1(X,Y) :- avenger(X),!,avenger(Y).
train2(X,Y) :- avenger(X),X\=Y,avenger(Y).
List all answers returned by the following queries.
train2(A, captainAmerica). %returns A=thor.
train2(captainAmerica, A). %returns false.
My question is about the second query. Why wouldn't this return A=thor. ? I messed around a bit and if i change train2 to
train2(X,Y) :- avenger(X),avenger(Y),X\=Y.
when i run the second query I get
A=thor.
A quick explanation of why the order of the rules in the query matters here would be awesome. Thanks.
\=
is a weird predicate... It says, "if the unification of the two arguments succeeds, fail; if the unification fails, succeed". So, as the unification of a free variable with an atom will always succeed, it fails.Once the
Y
has been unified withthor
, the unification ofcaptainAmerica
withthor
fails, so theX \= Y
succeeds.Anyway, you should not use
\=
in this context. Instead, usedif/2
. Try messing around with a predicate defined as:Better than the other two in several ways. You can search SO for other questions with
dif/2
.