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, X
of 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?
Rally source
share