I have no idea why my function doesn't work. I have gone through all the posts about non-exhaustive functions but my functions fulfills all possible options as far as I can see.
ascending :: [Int] -> Bool
ascending [] = error "Empty list given"
ascending [x] = True
ascending [x,y] | y>=x = True
| x<y = False
ascending (x:y:xs) | y>=x = (ascending (y:xs))
| x<y = False
Result:
*Main> ascending []
*** Exception: Empty list given
*Main> ascending [1]
True
*Main> ascending [1, 2]
True
*Main> ascending [2, 1]
*** Exception: test01.hs:(51,1)-(56,55): Non-exhaustive patterns in function ascending
It works for one pair, but not if the pair is not ascending. When I follow my code it should be just returning False.
[2, 1]
? – Falsterotherwise
orTrue
, then the case branch is considered not exhaustive, and unless there's another branch below catching the potentially non-matched terms, it will trigger the warning. IMO, I'd like this to be a compile-time error (as in Agda, etc.) so to force the programmer to write all the cases, even if some of them are simplyundefined
. – Overton