Skip to content

Instantly share code, notes, and snippets.

@lgastako
Created January 7, 2020 16:13
Show Gist options
  • Save lgastako/6290f1d23309043930abc739d563b676 to your computer and use it in GitHub Desktop.
Save lgastako/6290f1d23309043930abc739d563b676 to your computer and use it in GitHub Desktop.
data Suit = Spades | Hearts | Clubs | Diamonds
deriving Show
data Rank = Ace | Two | Three | Four | Five | Six | Seven
| Eight | Nine | Ten | Jack | Queen | King
deriving Show
data Card = Card
{ rank :: Rank
, suit :: Suit
} deriving Show
parseCard :: String -> Maybe Card
parseCard (r:s:[]) = do
r <- rankFrom r
s <- suitFrom s
pure $ Card r s
parseCard _ = Nothing
rankFrom :: Char -> Maybe Rank
rankFrom 'A' = Just Ace
rankFrom '2' = Just Two
-- ...
rankFrom 'K' = Just King
rankFrom _ = Nothing
suitFrom :: Char -> Maybe Suit
suitFrom 'S' = Just Spades
suitFrom 'H' = Just Hearts
suitFrom 'C' = Just Clubs
suitFrom 'D' = Just Diamonds
suitFrom _ = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment