From 74d32924a7ef4dd1f5da99c3ea995b4528dbdfd1 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sat, 16 Nov 2024 22:03:46 +0100 Subject: [PATCH] Improve code in Haskell solution for Problem 54 --- Haskell/p054.hs | 57 ++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/Haskell/p054.hs b/Haskell/p054.hs index 6efbcba..12140e7 100644 --- a/Haskell/p054.hs +++ b/Haskell/p054.hs @@ -46,14 +46,15 @@ import Data.List import Data.Function +import Data.Maybe import System.IO data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace - deriving (Eq, Ord, Show, Read, Bounded, Enum) + deriving (Eq, Ord, Show, Bounded, Enum) data Suit = Hearts | Diamonds | Clubs | Spades deriving (Eq, Show) -data Card = Card { value :: Value - , suit :: Suit +data Card = Card { value :: Maybe Value + , suit :: Maybe Suit } deriving (Eq, Show) instance Ord Card where @@ -65,26 +66,28 @@ data PokerGame = PokerGame { player1 :: Hand , player2 :: Hand } deriving (Eq, Show) -getValue :: Char -> Value -getValue '2' = Two -getValue '3' = Three -getValue '4' = Four -getValue '5' = Five -getValue '6' = Six -getValue '7' = Seven -getValue '8' = Eight -getValue '9' = Nine -getValue 'T' = Ten -getValue 'J' = Jack -getValue 'Q' = Queen -getValue 'K' = King -getValue 'A' = Ace +getValue :: Char -> Maybe Value +getValue '2' = Just Two +getValue '3' = Just Three +getValue '4' = Just Four +getValue '5' = Just Five +getValue '6' = Just Six +getValue '7' = Just Seven +getValue '8' = Just Eight +getValue '9' = Just Nine +getValue 'T' = Just Ten +getValue 'J' = Just Jack +getValue 'Q' = Just Queen +getValue 'K' = Just King +getValue 'A' = Just Ace +getValue _ = Nothing -getSuit :: Char -> Suit -getSuit 'H' = Hearts -getSuit 'D' = Diamonds -getSuit 'C' = Clubs -getSuit 'S' = Spades +getSuit :: Char -> Maybe Suit +getSuit 'H' = Just Hearts +getSuit 'D' = Just Diamonds +getSuit 'C' = Just Clubs +getSuit 'S' = Just Spades +getSuit _ = Nothing readCard :: String -> Card readCard (v:s:_) = Card{value=getValue v,suit=getSuit s} @@ -95,14 +98,14 @@ readGame g = PokerGame{player1=map readCard (take 5 (words g)), player2=map read royalFlush :: Hand -> Bool royalFlush h = let hs = sort h - in length (head (groupBy ((==) `on` suit) hs)) == 5 && [Ten .. Ace] == map value hs + in length (head (groupBy ((==) `on` suit) hs)) == 5 && [Ten .. Ace] == map (fromJust . value) hs straightFlush :: Hand -> Bool straightFlush h = let hs@(x:_) = sort h - start = value x + start = fromJust . value $ x in length (head (groupBy ((==) `on` suit) hs)) == 5 && - take 5 [start..] == map value hs + take 5 [start..] == map (fromJust . value) hs fourOfAKind :: Hand -> Bool fourOfAKind h = @@ -121,8 +124,8 @@ flush h = straight :: Hand -> Bool straight h = let hs@(x:_) = sort h - start = value x - in take 5 [start..] == map value hs + start = fromJust . value $ x + in take 5 [start..] == map (fromJust . value) hs three :: Hand -> Bool three h =