I'm going through the 99 Haskell problems and I have a few question about the solution I implemented for Problem 23.
Extract a given number of randomly selected elements from a list.
This is the code I came up with:
-- Extract a given number of randomly selected elements from a list. import System.Random randomFromList :: StdGen -> Int -> [a] -> ([a], StdGen) randomFromList generator 0 _ = (, generator) randomFromList generator toDo xs = let (nextInt, generator') = next generator index = mod nextInt (length xs) (otherNumbers, generator'') = randomFromList generator' (toDo -1) xs in ((xs !! index) : otherNumbers, generator'')
It works, but I'm not sure this is idiomatic Haskell nor that it is the best way to address this problem.
None of the solutions in the Haskell wiki uses the
let approach so I was wondering if it is discouraged for some reason.