I was trying out the following example given in this tutorial.
What I could not understand is how the case guards for the cases 'c' and _ don’t use the on at all?
I modified the code as given here, but then when I run it I got the error “Non-exhaustive patterns in case”:
*StateGame> main "accaaaa"
*** Exception: state1.hs:(27,5)-(31,36): Non-exhaustive patterns in case
Why is this so?
A case guard is a boolean expression which is checked after successfully matching the corresponding pattern. If it evaluates to
True, that branch is chosen. Otherwise, Haskell will keep trying each case from top to bottom.In your example, all the cases have the guard expression
on:Thus, when
onisFalse, none of the branches can be chosen, so you get a “Non-exhaustive patterns in case” exception.