07/01/13 17:10:31
>>465
>>467の人のようにこういう操作はArrayの方が得意だけど無理矢理やってみた
手抜きして右からまわしてる
import Random
-- main = print [1,2,3] >> hogeIO [1,2,3] >>= print
hogeIO :: [a] -> IO [a]
hogeIO xs = getStdRandom (hoge xs)
hoge :: (RandomGen g) => [a] -> g -> ([a], g)
hoge xs g = foldr iter ([],g) xs
where
iter x (xs,g) = (x':xs', g')
where
((x', _:xs'), g') = replaceR x (x:xs) g
replaceR :: (RandomGen g) => a -> [a] -> g -> ((a, [a]), g)
replaceR x xs g = (replace i x xs, g')
where
(i,g') = randomR (0, length xs - 1) g
replace :: Int -> a -> [a] -> (a,[a])
replace n x xs = (z, ys ++ x:zs)
where
(ys, z:zs) = splitAt n xs