diff options
author | Ben Sima <ben@bsima.me> | 2018-08-07 10:57:08 -0700 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2018-08-07 10:57:08 -0700 |
commit | 6db87806aadc680edaa37a0143a60525629b660e (patch) | |
tree | 12cb4843a2374c1733cbb3e448f188c631ddc0ae | |
parent | 685ac4cd483941e1013d9fc1911129987f653eea (diff) |
WIP from last week
-rwxr-xr-x | rain/main.hs | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/rain/main.hs b/rain/main.hs index 41ab03b..0133c2b 100755 --- a/rain/main.hs +++ b/rain/main.hs @@ -8,6 +8,7 @@ -} {-# LANGUAGE TypeSynonymInstances #-} +{-# LANGUAGE FlexibleInstances #-} {- @@ -48,25 +49,41 @@ data Suit | Diamond | Heart | Spade - deriving (Show, Enum) + deriving (Show, Enum, Bounded) suits :: [Suit] suits = [ Club .. ] -allCards :: [(Name, Suit)] +type Card = (Name, Suit) + +instance Random Suit where + randomR (a, b) g = + case randomR (fromEnum a, fromEnum b) g of + (x, g') -> (toEnum x, g') + random g = randomR (minBound, maxBound) g + +instance Random Name where + randomR (a, b) g = + case randomR (fromEnum a, fromEnum b) g of + (x, g') -> (toEnum x, g') + random g = randomR (minBound, maxBound) g + +-- Dot product is same as 'liftA2 (,)' +allCards :: [Card] allCards = liftA2 (,) names suits -data CardsGen = CardsGen - deriving (Show) +shuffle' :: [Card] -> _ +shuffle' (i:is) xs = (last firsts) : shuffle' is (init firsts ++ rest) + where (firsts, rest) = splitAt (i `mod` length xs) xs ---instance RandomGen CardsGen where --- genRange _ = (0, cardsLength) --- next CardsGen = stdNext (StdGen 0 cardsLength) --- split = stdSplit (StdGen 0 cardsLength) -suffle :: [cards] -> [cards] -suffle arr = undefined +shuffle g xs = shuffle (randoms g) xs main :: IO [()] main = do - sequence $ map (putStrLn.show) allCards + g <- getStdGen + let namesLength = length names + let randomNames = map fromEnum $ take namesLength $ randomRs (0, namesLength-1) g + let suitsLength = length suits + let randomSuits = map fromEnum $ take suitsLength $ randomRs (0, suitsLength-1) g + sequence $ map (putStrLn.show) $ liftA2 (,) randomNames randomSuits |