From 6db87806aadc680edaa37a0143a60525629b660e Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Tue, 7 Aug 2018 10:57:08 -0700 Subject: WIP from last week --- rain/main.hs | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'rain') 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 -- cgit v1.2.3