Talk:99 questions/Solutions/23

From HaskellWiki
Jump to navigation Jump to search

The more elegant solution using randomRs does not save the new random state and is thus dangerous to use if randomness is important.

some notes

the signature of removeAt is:

   removeAt :: Int -> [a] -> (a, [a])

so we have to swap the orders of parameters in the calls of removeAt from:

   removeAt l (k+1)

to:

   removeAt (k+1) l

and we have to extract the remaining list from the result as well:

   snd $ removeAt (k+1) l

so the full version should be:

   rnd_select2 :: RandomGen g => [a] -> Int -> g -> ([a], g)
   rnd_select2 _ 0 gen = ([], gen)
   rnd_select2 [] _ gen = ([], gen)
   rnd_select2 l count gen
      | count == (length l) = (l, gen)
      | otherwise           =  rnd_select2 (snd $ removeAt (k+1) l) count gen'
                           where (k, gen') =
                                   randomR (0, (length l) - 1) gen
   rnd_selectIO :: [a] -> Int -> IO [a]
   rnd_selectIO l count = getStdRandom $ rnd_select2 l count