# Difference between revisions of "Talk:99 questions/Solutions/23"

From HaskellWiki

m (→some notes) |
m (→some notes) |
||

Line 5: | Line 5: | ||

the signature of removeAt is: |
the signature of removeAt is: |
||

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

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

removeAt l (k+1) |
removeAt l (k+1) |
||

to: |
to: |

## Latest revision as of 04:13, 27 July 2017

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