tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
27
Task/Playing-cards/Haskell/playing-cards.hs
Normal file
27
Task/Playing-cards/Haskell/playing-cards.hs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import System.Random
|
||||
|
||||
data Pip = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten |
|
||||
Jack | Queen | King | Ace
|
||||
deriving (Ord, Enum, Bounded, Eq, Show)
|
||||
|
||||
data Suit = Diamonds | Spades | Hearts | Clubs
|
||||
deriving (Ord, Enum, Bounded, Eq, Show)
|
||||
|
||||
type Card = (Pip, Suit)
|
||||
|
||||
fullRange :: (Bounded a, Enum a) => [a]
|
||||
fullRange = [minBound..maxBound]
|
||||
|
||||
fullDeck :: [Card]
|
||||
fullDeck = [(pip, suit) | pip <- fullRange, suit <- fullRange]
|
||||
|
||||
insertAt :: Int -> a -> [a] -> [a]
|
||||
insertAt 0 x ys = x:ys
|
||||
insertAt n _ [] = error "insertAt: list too short"
|
||||
insertAt n x (y:ys) = y : insertAt (n-1) x ys
|
||||
|
||||
shuffle :: RandomGen g => g -> [a] -> [a]
|
||||
shuffle g xs = shuffle' g xs 0 [] where
|
||||
shuffle' g [] _ ys = ys
|
||||
shuffle' g (x:xs) n ys = shuffle' g' xs (n+1) (insertAt k x ys) where
|
||||
(k,g') = randomR (0,n) g
|
||||
Loading…
Add table
Add a link
Reference in a new issue