The following two functions behave differently when given an empty string:
guardMatch l@(x:xs)
| x == '-' = "negative " ++ xs
| otherwise = l
patternMatch ('-':xs) = "negative " ++ xs
patternMatch l = l
Here my output:
*Main> guardMatch ""
"*** Exception: matching.hs:(1,1)-(3,20): Non-exhaustive patterns in function guardMatch
*Main> patternMatch ""
""
Question: why does not the ‘otherwise’ close catch the empty string?
The
otherwiseis within the scope of the patternl@(x:xs), which can only match a non-empty string. It might help to see what this (effectively) translates to internally:(Actually, I think the
ifis translated to acase+ guard instead of the other way around.)