Home > Mobile >  Haskell: Value Function
Haskell: Value Function

Time:11-21

I'm still doing the exercise from the book and the one exercise says that I should create a function: value :: Hand -> Int, which returns the value of the hand.

My code so far looks like this:

data Card = Card Rank Suit 
            deriving (Show)

data Suit = Spade | Heart | Diamond | Club
            deriving (Show, Ord, Eq, Enum)

data Rank = Ace | King | Queen | Jack | Ten | Nine | Eight | Seven
            deriving (Show, Eq, Ord, Enum)

data Hand = PairOf Rank | ThreeOf Rank | Flush Suit
            deriving (Show, Eq)

-- This function implements a value function which returns the value of the hand.
-- Pair (2 cards of the same rank) = 1
-- ThreeOf (3 cards of the same rank) = 2
-- Flush (3 cards of the same suit) = 3
-- Nothing = 0

value :: Hand -> Int
value (PairOf r1) = 1
value (Drilling r1) = 2
value (Flush s1) = 3
otherwise = 0

I now have another problem, because I don't know how to describe "Nothing". Nothing is described here as a possible hand combination in which Pair, ThreeOf and Flush do not occur. Would otherwise = 0 work or doesn't that make much sense?

Thanks again for the suggestions, I corrected them! Thanks also in advance for explanations and help.

CodePudding user response:

otherwise won't work here. What you want is an irrefutable pattern that will match anything, and further since you don't care what gets matched, you specifically want the pattern _:

value :: Hand -> Int
value (PairOf r1) = 1
value (ThreeOf r1) = 2
value (Flush s1) = 3
value _ = 0

Since you don't care about what kind of pair, etc you get, you can also use _ in the other cases:

value :: Hand -> Int
value (PairOf _) = 1
value (ThreeOf _) = 2
value (Flush _) = 3
value _ = 0

If you wanted to match Nothing (or whatever name you come up with that doesn't conflict with the Maybe constructor) specifically, it's just

value Nothing = 0

CodePudding user response:

One of the problems you might be having is that there is no Nothing.

Here's the full type of Hands for holdem:

data HandRank
  = HighCard Rank Rank Rank Rank Rank
  | OnePair Rank Rank Rank Rank
  | TwoPair Rank Rank Rank
  | ThreeOfAKind Rank Rank Rank
  | Straight Rank
  | Flush Rank Rank Rank Rank Rank
  | FullHouse Rank Rank
  | FourOfAKind Rank Rank
  | StraightFlush Rank
  deriving (Eq, Ord, Show, Generic)

The extra ranks in the data type are there to distinguish hands, so a pair of aces with a King & Queen kicker beats a pair of aces with a King & Jack kicker. But otherwise, it's the same as your setup.

Once you have a complete transformation from a set of cards to a hand value, there is no nothing. What you might be thinking of as nothing is the HighHand line in the sum type. If your context is that's it for possible hands, then I would add a NoValue to the Hand sum type, as better than outputting a Maybe Hand.

Using the wild cards otherwise _ or value _ introduces a chance of a bug because you might forget about coding up a full house, say, and your existing function would work. If you forget to code one of the sum types, and you don't have a match-any pattern, the compiler will complain. Providing the compiler with all branches of a sum type is also a hot development area of GHC, and it will be fast.

  • Related