Skip to content

Instantly share code, notes, and snippets.

@jpfuentes2
Created July 30, 2014 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpfuentes2/1e9182c746e8848b587a to your computer and use it in GitHub Desktop.
Save jpfuentes2/1e9182c746e8848b587a to your computer and use it in GitHub Desktop.
{-# 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
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