Pattern Matching - Prolog vs. Haskell

前端 未结 5 1848
再見小時候
再見小時候 2021-02-02 07:42

This is not a homework question, rather an exam study guide question. What is the difference between pattern matching in Prolog Vs Haskell?

I\'ve done some research and

5条回答
  •  孤街浪徒
    2021-02-02 07:58

    Here is an example I find interesting in Prolog to support @chac (+1 btw) mention about how Prolog's unification "builds" terms that we ran into in the prolog tag yesterday:

    swapPairs([],       []).
    swapPairs([X],      [X]).
    swapPairs([X, Y|T], [Y, X|R]) :- swapPairs(T, R).
    

    This predicate has almost no "body". It only uses unification of its arguments in its head and recursion.

    As pointed out by both @chac and @LeleDumbo, that's because Prolog's unification is "both-way".

    Here is what A gentle Introduction to Haskell says about it:

    Pattern matching in Haskell is different from that found in logic programming languages such as Prolog; in particular, it can be viewed as "one-way" matching, whereas Prolog allows "two-way" matching (via unification), along with implicit backtracking in its evaluation mechanism.

提交回复
热议问题