Lunuy
Lunuy

Reputation: 469

Prolog - equal operator wrapping

equal(a, b) :- (a = b).

I defined equal.

1 ?- [index].
true.

2 ?- equal(1, 1).
false.

3 ?- 1 = 1.
true.

When I run

equal(1, 1)

it returns false.

Why does it return false and how can i fix it?

Upvotes: 0

Views: 185

Answers (1)

Dennis
Dennis

Reputation: 46

First of all, you should write equal(A, B) :- A = B instead of what you've written. The difference is that a and b are constants and A and B are variables that can be unified with values. I guess then it should work for your example.

But you should note that "=" predicate just tries to unify its arguments. So when you ask "1 = 1" the result is true because 1 unifies with 1. But when you ask "2 + 2 = 4" (or equal(2 + 2, 4)) the result will be false because this operator does not evaluate arithmetic operations. If comparing arithmetic expressions is what you want to do use =:= operator instead:

equal(A, B) :- A =:= B.

Upvotes: 2

Related Questions