Random Permutations

Markus.Schnell@infineon.com Markus.Schnell@infineon.com
Thu, 6 Mar 2003 10:32:57 +0100


Is there a library routine for random permutations?

I didn't find any and did a quick hack, which works fine for my
application (length of list < 100), but what would be a more
elegant way?

> permute :: StdGen -> [a] -> [a]
> permute gen []  = []
> permute gen xs  = (head tl) : permute gen' (hd ++ tail tl)
>    where (idx, gen') = randomR (0,length xs - 1) gen
>          (hd,  tl)   = splitAt idx xs

Markus

--
Markus Schnell, Infineon Technologies AG