summaryrefslogtreecommitdiff
path: root/rain
diff options
context:
space:
mode:
Diffstat (limited to 'rain')
-rwxr-xr-xrain/main.hs39
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