summaryrefslogtreecommitdiff
path: root/System/Random/Shuffle.hs
diff options
context:
space:
mode:
Diffstat (limited to 'System/Random/Shuffle.hs')
-rw-r--r--System/Random/Shuffle.hs13
1 files changed, 6 insertions, 7 deletions
diff --git a/System/Random/Shuffle.hs b/System/Random/Shuffle.hs
index 774e7b4..d3cd387 100644
--- a/System/Random/Shuffle.hs
+++ b/System/Random/Shuffle.hs
@@ -25,8 +25,7 @@ module System.Random.Shuffle
where
import Control.Monad
- ( liftM,
- liftM2,
+ ( liftM2,
)
import Control.Monad.Random
( MonadRandom,
@@ -49,13 +48,13 @@ data Tree a
-- | Convert a sequence (e1...en) to a complete binary tree
buildTree :: [a] -> Tree a
-buildTree = (fix growLevel) . (map Leaf)
+buildTree = fix growLevel . map Leaf
where
growLevel _ [node] = node
growLevel self l = self $ inner l
inner [] = []
inner [e] = [e]
- inner (e1 : e2 : es) = e1 `seq` e2 `seq` (join e1 e2) : inner es
+ inner (e1 : e2 : es) = e1 `seq` e2 `seq` join e1 e2 : inner es
join l@(Leaf _) r@(Leaf _) = Node 2 l r
join l@(Node ct _ _) r@(Leaf _) = Node (ct + 1) l r
join l@(Leaf _) r@(Node ct _ _) = Node (ct + 1) l r
@@ -70,7 +69,7 @@ shuffle elements = shuffleTree (buildTree elements)
where
shuffleTree (Leaf e) [] = [e]
shuffleTree tree (r : rs) =
- let (b, rest) = extractTree r tree in b : (shuffleTree rest rs)
+ let (b, rest) = extractTree r tree in b : shuffleTree rest rs
shuffleTree _ _ = error "[shuffle] called with lists of different lengths"
-- Extracts the n-th element from the tree and returns
-- that element, paired with a tree with the element
@@ -99,7 +98,7 @@ shuffle' elements len = shuffle elements . rseq len
-- independent sample from a uniform random distribution
-- [0..n-i]
rseq :: RandomGen gen => Int -> gen -> [Int]
- rseq n = fst . unzip . rseq' (n - 1)
+ rseq n = map fst . rseq' (n - 1)
where
rseq' :: RandomGen gen => Int -> gen -> [(Int, gen)]
rseq' 0 _ = []
@@ -111,7 +110,7 @@ shuffle' elements len = shuffle elements . rseq len
shuffleM :: (MonadRandom m) => [a] -> m [a]
shuffleM elements
| null elements = return []
- | otherwise = liftM (shuffle elements) (rseqM (length elements - 1))
+ | otherwise = shuffle elements <$> rseqM (length elements - 1)
where
rseqM :: (MonadRandom m) => Int -> m [Int]
rseqM 0 = return []