Why do my pattern matches inside a do block overlap?
(q, m) <- newRq
let m2 = appendMsg "first" key m
(q4, m4) = case m2 of
m -> deleteRec key q m2
_ -> (q, m2)
This compiles with the warning
Warning: Pattern match(es) are overlapped
In a case alternative: _ -> ...
and does not work as I want to. It just seems that for (q4, m4) it always returns
[], fromList []
disregarding what the values of m2 and m are. Is there any local variables where I do not expect them?
What I want to achieve in words: If m2 and m are equal then (q4, m4) should evaluate to deleteRec key q m2, otherwise to (q, m2).
The first pattern in the
case(m) matches everything and assigns it tom. The second one matches everything and discards it (_), but has nothing left to match becausemwill get everything.I think you meant for the case to work like a
switchstatement, but it actually works as a set of patterns, much like a function declaration. So yourcaseis the same as something like:In this code, you’re probably best off just using an
if:You might also consider indenting the
ifstatement differently:should also work.
However, in general, you can actually use guards in a
casestatement, so this would work too:This is obviously harder to read than an
if, but if you had more branches or needed to do some actual pattern matching, it would make sense.