Difference between revisions of "Talk:99 questions/Solutions/23"
Jump to navigation
Jump to search
(Created page with "The more elegant solution using randomRs does not save the new random state and is thus dangerous to use if randomness is important.") |
m (→some notes) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
The more elegant solution using randomRs does not save the new random state and is thus dangerous to use if randomness is important. |
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 |
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