new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
20
Task/Balanced-brackets/Haskell/balanced-brackets-2.hs
Normal file
20
Task/Balanced-brackets/Haskell/balanced-brackets-2.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
module VShuffle (shuffle) where
|
||||
|
||||
import Data.List (mapAccumL)
|
||||
import System.Random
|
||||
import Control.Monad.ST
|
||||
import qualified Data.Vector as V
|
||||
import qualified Data.Vector.Generic.Mutable as M
|
||||
|
||||
-- Generate a list of array index pairs, each corresponding to a swap.
|
||||
pairs :: (Enum a, Random a, RandomGen g) => a -> a -> g -> [(a, a)]
|
||||
pairs l u r = snd $ mapAccumL step r [l..pred u]
|
||||
where step r i = let (j, r') = randomR (i, u) r in (r', (i, j))
|
||||
|
||||
-- Return a random permutation of the list. We use the algorithm described in
|
||||
-- http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm.
|
||||
shuffle :: (RandomGen g) => [a] -> g -> [a]
|
||||
shuffle xs r = V.toList . runST $ do
|
||||
v <- V.unsafeThaw $ V.fromList xs
|
||||
mapM_ (uncurry $ M.swap v) $ pairs 0 (M.length v - 1) r
|
||||
V.unsafeFreeze v
|
||||
Loading…
Add table
Add a link
Reference in a new issue