Home > Enterprise >  Pattern match(es) are non-exhaustive in Haskell function
Pattern match(es) are non-exhaustive in Haskell function

Time:09-17

I've read similar questions about this warning in Haskell, but these generally envolve lists and mine doesn't:

teste :: Char -> Char -> Int
teste a b
   |ord (toUpper a) < ord (toUpper b) = 1
   |ord (toUpper a) > ord (toUpper b) = 2
   |ord a > ord (toUpper b) = 3
   |ord a < ord (toUpper b) = 4

Why the "pattern matches" warning is showing up?

Pattern match(es) are non-exhaustive
In an equation for `teste': Patterns not matched: _ _

Is there something I can do?

Edit: The new function's code:

alphabetOrder :: Char -> Char -> Char 
alphabetOrder a b =  case compare (toUpper a) (toUpper b) of LT -> a; GT -> b; EQ -> a

CodePudding user response:

What should the result be when ord a == ord (toUpper b)? You haven't specified that, and that's a real possibility.

But then, even if you did specify that, the compiler still wouldn't be able to tell that >, <, and == cover all possible cases. For the compiler, >, <, and == are just some functions, it can't prove that one of them will always return True.

So what you really need to do is add a case that will be matched when no other case matches:

teste :: Char -> Char -> Int
teste a b
   |ord (toUpper a) < ord (toUpper b) = 1
   |ord (toUpper a) > ord (toUpper b) = 2
   |ord a > ord (toUpper b) = 3
   |ord a < ord (toUpper b) = 4
   |otherwise = 5
  • Related