1

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
Zach L
  • 16,072
  • 4
  • 38
  • 39
Larissa
  • 13
  • 3

3 Answers3

5

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?

Dan
  • 10,990
  • 7
  • 51
  • 80
  • Well, since you didn't include many comments, I'm having a hard time understanding what you really want. That's why I'm asking - say in your own words what you want `eval` to really do, separate from the implementation of it. – Dan Jun 22 '11 at 22:21
  • Oh, okay. I just want eval to take two integers, and see if they're equal. If they are, then true. If they're not, then false. It seems so simple, but I don't understand how x and y are booleans here? – Larissa Jun 22 '11 at 22:25
  • Ok, step back a moment. If that's what you really want, `eval` would look like this: `eval x y = x == y` and you wouldn't need the `Value` datatype. But again, I get the feeling that's not what you really want. – Dan Jun 22 '11 at 22:34
  • In the eval that you described, how would you return a true or false value based on a test of 2 numbers? In order to better learn Haskell I have been working to develop an integer calculator, and wanted to include this function in there as well, to work on putting together different types of functions. For example, if I used a test of t1 = Numequal (ConstInt 1)(ConstInt 1), and then t2 = eval t1? – Larissa Jun 22 '11 at 22:44
  • 1
    Okay, I think I may have figured out where I went wrong. (Quite a few places!) I took out the eval on x and y, so that it would just provide me with #s, and replaced the Value datatype. Oh...and I switched the True/False, so that they actually make sense!:) – Larissa Jun 22 '11 at 22:48
  • I thought my name would be unique if I just stuck around Haskell questions. Guess I was wrong. ;) – Dan Burton Jun 22 '11 at 23:15
4

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

eval (Numequal e1 e2) = ...
eval _ = False -- or patterns for ConstInt, Ctrue and Cfalse
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • Oh, of course! Thank you...When considering what could be entered I was, for some reason, only thinking in my head true/false. – Larissa Jun 22 '11 at 22:07
  • Well, that's actually not true in Haskell. It's just that if you don't, runtime errors might result if you're not careful. – Dan Jun 22 '11 at 22:08
0

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

mhitza
  • 5,709
  • 2
  • 29
  • 52