Created
July 30, 2014 16:01
-
-
Save jpfuentes2/1e9182c746e8848b587a 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
{-# LANGUAGE DeriveDataTypeable #-} | |
import Control.Monad.Random | |
import System.Random.Shuffle | |
import Data.Typeable | |
import Data.Data | |
data Suit = Hearts | Clubs | Diamonds | Spades | |
deriving (Eq, Enum, Bounded, Ord, Data, Typeable) | |
data Value = Ace | King | Queen | Jack | Number Int | |
deriving (Eq, Data, Typeable) | |
newtype Card = Card (Suit, Value) | |
type Deck = [Card] | |
headCharOfType :: Data a => a -> String | |
headCharOfType a = [head $ show $ toConstr a] | |
instance Show Card where | |
show (Card (s, Number x)) = headCharOfType s ++ ":" ++ show x | |
show (Card (s, v)) = headCharOfType s ++ ":" ++ headCharOfType v | |
deck :: Deck | |
deck = zipWith card suits values | |
where reps n a = concat $ replicate n a | |
card s v = Card (s, v) | |
values = reps 4 $ [Ace, King, Queen, Jack] ++ map Number [2..10] | |
suits = reps 16 [Hearts .. Spades] | |
main = do | |
deck <- evalRandIO $ shuffleM $ concat $ replicate 1 deck | |
print deck |
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
import Control.Monad.Random | |
import System.Random.Shuffle | |
data Suit = Hearts | Clubs | Diamonds | Spades | |
deriving (Eq, Enum, Bounded, Ord) | |
data Value = Ace | King | Queen | Jack | Number Int | |
deriving (Eq) | |
newtype Card = Card (Suit, Value) | |
type Deck = [Card] | |
instance Show Suit where | |
show Hearts = "H" | |
show Clubs = "C" | |
show Diamonds = "D" | |
show Spades = "S" | |
instance Show Value where | |
show Ace = "A" | |
show King = "K" | |
show Queen = "Q" | |
show Jack = "J" | |
show (Number x) = show x | |
instance Show Card where | |
show (Card (s, v)) = show s ++ show v | |
deck :: Deck | |
deck = zipWith card suits values | |
where reps n a = concat $ replicate n a | |
card s v = Card (s, v) | |
values = reps 4 $ [Ace, King, Queen, Jack] ++ map Number [2..10] | |
suits = reps 16 [Hearts .. Spades] | |
main = do | |
deck <- evalRandIO $ shuffleM deck | |
print deck |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment