No title

Anonymous Coward 2018-02-12 13:15:40.137239 UTC

1{--
2Shuffle a IO array
3--}
4
5import System.Random
6import Control.Monad
7import Data.Array.IO
8
9aShuffle :: [a] -> IO [a]
10aShuffle xs = do
11 let
12 listLength = length xs
13 newArray :: Int -> [a] -> IO (IOArray Int a)
14 newArray n xs = newListArray (1, listLength) xs
15 array <- newArray listLength xs
16 forM [1..listLength] $ \i -> do
17 j <- randomRIO (i, listLength)
18 valI <- readArray array i
19 valJ <- readArray array j
20 writeArray array j valI
21 return valJ
22
23main = do
24 xs <- aShuffle ["abcde"]
25 print xs
26
27
28{--
29
30runhaskell shuffleList.hs
31["abcde"]
32
33--}