Comparison of patterns in Haskell and Erlang

I have a small Erlang function that compares if two lists are equal:

myEq([], [])         -> true;
myEq([X|Xs], [X|Ys]) -> myEq(Xs, Ys);
myEq(_, _)           -> false.

The comparison is performed in line 2, Xof is [X|Xs]always attached to the first element of the first list, it [X|Ys]matches only if the first elements of both lists are equal.

If I try this in Haskell, I get an error: "Conflict definitions for X." Possible solution in Haskell:

myEq (x:xs) (y:ys) = if x == y then myEq xs ys else False

But I would like to know if this can be done in Haskell using pattern matching?

+6
source share
2 answers

, Haskell x . Haskell , , , Prolog Erlang do. Haskell '98:

, , --- .

(, )

- ( , ). , :

myEq [] [] = True
myEq (x:xs) (y:ys) | x == y = myEq xs ys
--                 | otherwise = False
myEq _ _ = False

otherwise , Haskell , , False.

, , :

myEq [] [] = True
myEq (x:xs) (y:ys) = x == y && myEq xs ys
myEq _ _ = False

, , , x y, , , . , , .

+10

. . , .

+1

Source: https://habr.com/ru/post/1015120/


All Articles