Anonymous user
Poker hand analyser: Difference between revisions
m
→{{header|Haskell}}
Line 1,963:
=={{header|Haskell}}==
===Basic Version===
<lang Haskell>
import Data.Maybe (mapMaybe)▼
import Data.Function (on)
import Data.List (group, nub, any, sort, sortBy)
▲import Data.Maybe (mapMaybe)
import Text.Read (readMaybe)
data Suit = Club | Diamond | Spade | Heart deriving (Show, Eq)
Line 1,973 ⟶ 1,976:
deriving (Show, Eq, Enum, Ord, Bounded)
data Card = Card { suit :: Suit, rank :: Rank } deriving (Show, Eq)
, rank :: Rank▼
instance Read Suit where
} deriving (Show, Eq)▼
instance Read Rank where
instance Read Card where
readsPrec d = fmap (, "") . mapMaybe card . lex
card (r, s) = Card <$> (readMaybe s :: Maybe Suit)
-- Special hand
Line 1,985 ⟶ 2,016:
isSucc [x] = True
isSucc (x:y:zs) = (x /= maxBound && y == succ x) && isSucc (y:zs)
▲ where
▲ parseRank = case init xs of "a" -> Just Ace
▲ "2" -> Just Two
▲ "3" -> Just Three
▲ "4" -> Just Four
▲ "5" -> Just Five
▲ "6" -> Just Six
▲ "7" -> Just Seven
▲ "8" -> Just Eight
▲ "9" -> Just Nine
▲ "10" -> Just Ten
▲ "j" -> Just Jack
▲ "q" -> Just Queen
▲ "k" -> Just King
▲ _ -> Nothing
▲ parseSuit = case last xs of '♥' -> Just Heart
▲ '♦' -> Just Diamond
▲ '♣' -> Just Club
▲ '♠' -> Just Spade
▲ _ -> Nothing
nameHand :: String -> String
Line 2,023 ⟶ 2,029:
| otherwise = ": High card"
where
cards = mapMaybe
sortedRank = sort $ rank <$> cards
rankCounts = sortBy (compare `on` snd) $ (,) <$> head <*> length <$> group sortedRank
|