tolUene
tolUene

Reputation: 584

Haskell - Pattern matches overlap?

Why do the pattern matches overlap in this code:

(+) (Roman (_, [])) x = x
(+) x (Roman (_, [])) = x

It would make sense considering x + y is the same as y + x but I don't think haskell takes care of that, or does it?

Upvotes: 1

Views: 140

Answers (2)

molbdnilo
molbdnilo

Reputation: 66371

The problem is that x matches anything.

You can use at-patterns to make them unique:

(+) (Roman (_, [])) x@(Roman(_, _:_) = x
(+) x@(Roman(_, _:_) (Roman (_, [])) = x

Upvotes: 1

JP Moresmau
JP Moresmau

Reputation: 7393

x could be anything, so it could be Roman (_,[]).

Upvotes: 3

Related Questions