[Haskell-cafe] Linear shuffle

Gracjan Polak gracjan at acchsh.com
Fri Jan 14 03:17:41 EST 2005


Hi,

I want to implement linear list shuffle in Haskell 
(http://c2.com/cgi/wiki?LinearShuffle) and I invented code:

shuffle :: [a] -> IO [a]
shuffle [] = return []
shuffle x = do
     r <- randomRIO (0::Int,length x - 1)
     s <- shuffle (take r x ++ drop (r+1) x)
     return ((x!!r) : s)

This algorithm seems not effective, length, take, drop and (!!) are 
costly. Is there any better way to implement shuffle?

-- 
Gracjan


More information about the Haskell-Cafe mailing list