Larissa
Larissa

Reputation: 13

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

Upvotes: 1

Views: 302

Answers (3)

Dan
Dan

Reputation: 11089

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?

Upvotes: 5

Cat Plus Plus
Cat Plus Plus

Reputation: 129964

When using pattern matching, you have to handle all possible cases.

eval (Numequal e1 e2) = ...
eval _ = False -- or patterns for ConstInt, Ctrue and Cfalse

Upvotes: 4

mhitza
mhitza

Reputation: 5715

As far as I know your data type should be be deriving (Eq) for you to be able to make comparisons on it.

Upvotes: 0

Related Questions