module Main import System, Int, Char, String, List, Misc, MergeSort, ValueCast Start | flush cards | straight cards | 14 == num (head cards) = "Royal flush" = "Straight flush" = "Flush" | straight cards = "Straight" | 4 == head groups = "Four of a kind" | 3 == head groups | 2 == groups !! 1 = "Full house" = "Three of a kind" | 2 == head groups | 2 == groups !! 1 = "Two pair" = "One pair" = "No pair" where cards = sortBy (>) $ parseCards getCommandLine.[1] groups = sortBy (>) $ map length $ groupCards [[]] 0 cards :: Card = Card !Char !Int //suit num(2..14) instance < Card where (<) (Card s0 n0) (Card s1 n1) = n0 < n1 suit (Card s n) = s num (Card s n) = n parseCards t = p 0 where l = size t p i | i >= l = [] = [Card s n: p (i+2)] where s = t.[i] n = case t.[i+1] of 'A' = 14 'K' = 13 'Q' = 12 'J' = 11 'T' = 10 c = toInt (c - '0') groupCards ls _ [] = ls groupCards [l:ll] m [c=:Card s n: rest] | m == n = groupCards [[c:l]:ll] n rest = groupCards [[c],l:ll] n rest flush cards = and $ zipWith (==) suits (tail suits) where suits = map suit cards straight cards = (and $ zipWith (\a b = a == b + 1) nums (tail nums)) || caseAce nums where nums = map num cards caseAce [14,5,4,3,2] = True caseAce _ = False