[Haskell-cafe] Linear shuffle

Henning Thielemann iakd0 at clusterf.urz.uni-halle.de
Fri Jan 14 04:47:59 EST 2005


On Fri, 14 Jan 2005, Gracjan Polak wrote:

> 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?

Is it a good idea to use IO monad for this plain computation?



More information about the Haskell-Cafe mailing list