Can someone explain the difference between the ==
and the =
operator in Prolog? I know that X = Y
means X unifies with Y and is true i
= stands for unification, it means that it will try to bind the free variables to make them match the other members. for example : A = h(X) will turn A into the term h(X) if A is free, and will fail if A is bound to say 5. unification is great because you can do pattern matching with it, for example :
X-Y:Z = 5-[a, b, c]:y
will give you
X = 5, Y = [a, b, c] and Z = y
because prolog tries to make X-Y:Z fit the expression 5-[a, b, c]:y. It's very useful.
Note that unification is used when you call a predicate and some techniques ensue : say you want to return the value of an accumulator in a recursive predicate, you can do that :
recursive_predicate([], Accumulator, Accumulator).
recursive_predicate(Input, Accumulator, Output) :- %recursive stuff.
the first clause will try to unify the third and second argument, so if the third is free, it has now the same value as the second.
== is equality without trying to bind the variables.