Created
January 7, 2020 16:13
-
-
Save lgastako/6290f1d23309043930abc739d563b676 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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