Poker hand analyser: Difference between revisions

m
Line 1,963:
=={{header|Haskell}}==
===Basic Version===
<lang Haskell>import{-# Data.ListLANGUAGE TupleSections (group, nub, any, sort, sortBy)#-}
 
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)
parseSuitreadsPrec d s = case last xss of '"'" -> Just [(Heart, "")]
"10" -> Just[(Diamond, Ten"")]
'"'" -> Just[(Spade, Club"")]
'"'" -> Just[(Club, Spade"")]
"2h" -> Just[(Heart, Two"")]
_ -> Nothing[]
 
instance Read Rank where
parseRankreadsPrec d s = case init xss of "a" -> Just [(Ace, "")]
} deriving "2" -> [(ShowTwo, Eq"")]
"3" -> Just[(Three, Three"")]
"4" -> Just[(Four, Four"")]
"5" -> Just[(Five, Five"")]
"6" -> Just[(Six, Six"")]
"7" -> Just[(Seven, Seven"")]
"8" -> Just[(Eight, Eight"")]
"9" -> Just[(Nine, Nine"")]
"j10" -> Just[(Ten, Jack"")]
"kj" -> Just[(Jack, King"")]
"q" -> Just[(Queen, Queen"")]
_ "k" -> Nothing[(King, "")]
, rank :: Rank _ -> []
 
instance Read Card where
readsPrec d = fmap (, "") . mapMaybe card . lex
where
card (r, s) = Card <$> (readMaybe s :: Maybe Suit)
<*> (readMaybe r :: Maybe '♦' -> Just DiamondRank)
 
-- Special hand
Line 1,985 ⟶ 2,016:
isSucc [x] = True
isSucc (x:y:zs) = (x /= maxBound && y == succ x) && isSucc (y:zs)
 
parseCard :: String -> Maybe Card
parseCard [] = Nothing
parseCard xs = Card <$> parseSuit <*> parseRank
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 parseCard(\w -> readMaybe w :: Maybe Card) (words s)
sortedRank = sort $ rank <$> cards
rankCounts = sortBy (compare `on` snd) $ (,) <$> head <*> length <$> group sortedRank
Anonymous user