1

I've run into a little problem i can't seem to solve, been at it for a few hours now to no avail.

I've defined a datatype, Hand.

data Hand = Empty | Add Card Hand
        deriving (Eq, Show)

And another type i'm using:

data Card = Card { rank :: Rank, suit :: Suit }
        deriving (Eq, Show)

I'm trying to create an infix operator which will add two hands and return a third one.

(<+) :: Hand -> Hand -> Hand
(<+) (Add c1 h1) (empty) = (Add c1 h1)
(<+) hand1 (Add c2 hand2) = (Add c2 (hand1 <+ hand2))

When i run a quickcheck test on this to see wether it's associative or not i get an error after a few times. The error says:

BlackJack.hs:(71,1)-(72,53): Non-exhaustive patterns in function <+

The three arguments it returns to me are empty (Always this one as the first one) and then a large hand as a second and the third argument. For example: Image of error. Also, quickchest test looks something like ((hand1 <+ hand2) <+ hand3) == (hand1 <+ (hand2 <+ hand3)).

I'm guessing i've got not enough cases to handle every possible scenario? I just can't figure out what's gone wrong. :(

Please no direct solutions, hints will do!

Edit: Did as a helpful commenter suggested and added a case for when two hands are empty, which returns the empty hand. All works well now. :) And to clear up confusion, i had declared what a empty hand is earlier. Empty and empty are two defferent things in my code, and i used them 'correctly'. :)

Rewbert
  • 347
  • 1
  • 3
  • 11
  • 11
    `empty /= Empty`. Also, your left parameter can be empty and you don't match for that. – Bartek Banachewicz Sep 13 '14 at 14:51
  • 10
    You don't handle any case in which both hands are `Empty` – Kritzefitz Sep 13 '14 at 14:55
  • 1
    Instead of posting an image copy the *text* and paste it into the question in a code block. It's actually 1) easier to read 2) we can copy and paste the code that it's contained. In this case it doesn't matter, but it *does* matter in other circumstances. (also: titles shouldn't contains tags, and error messages are better formatted in code blocks). – Bakuriu Sep 13 '14 at 17:04
  • 2
    If you had compiled with `-Wall` the compiler would have told you that you have non exhaustive patterns. Moral of the story: always use `-Wall`. – user2407038 Sep 13 '14 at 17:43

0 Answers0