What is the difference between == and = in Prolog?

前端 未结 2 974
抹茶落季
抹茶落季 2020-12-08 09:44

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

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 10:38

    = 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.

提交回复
热议问题