Search code examples
haskellbooleannon-exhaustive-patterns

Non-exhaustive Error in Basic Haskell Function


I'm new to Haskell and trying to put together a simple function to check whether or not two numbers are equal. This compiles, but when I try out a test of the program, it says that this is non-exhaustive. I don't understand how it can be non-exhaustive with a boolean function? Thanks in advance:

data Value = ConstInt Int
           | Numequal Value Value
           | Ctrue Bool
           | Cfalse Bool
            deriving (Read, Show)
eval:: Value -> Bool

eval (Numequal e1 e2) =
   let x = eval e1
       y = eval e2
         in case (x, y) of
            (i1, i2)  -> 
                if x == y
                then False
                else True

Solution

  • You haven't finished your eval function. For example, suppose I call eval (ConstInt 34). What should it return?

    Also, think about what's in the body of your function. eval returns a Boolean, so both x and y will be Booleans and you're testing to see if they're equal. Is that what you want?