Wed, 8 Aug 2001 19:59:54 +0200
On Thu, Aug 09, 2001 at 12:10:12AM +1000, Vincent Tseng wrote:
> does anyone know how to do this ?
> a function that will "randomly" takes elements from a list
> eg randomPick 3 ['a','b','c','d','e'] = ['d','a','a']
Your example suggests a type signature like:
randomPick :: Int -> [a] -> [a]
This isn't possible in that way. A pure Haskell function must
return the same thing on *every* invocation.
However, it were possible to either
* thread a random generator through randomPick, giving it the
randomPick :: (RandomGen g) => g -> Int -> [a] -> ([a], g)
* Make randomPick impure, using an IO type:
randomPick :: Int -> [a] -> IO [a]
To do that, try one of the following:
randomPickIO :: Int -> [a] -> IO [a]
randomPickIO howMany l = randomPickIO' howMany l (length l)
randomPickIO' howMany l@(hd:tl) len =
| howMany == 0 = return 
| len < howMany = error "randomPickIO: too many items to pick, too few items present"
| len == howMany = return l
| otherwise = do
r <- randomRIO (0, len - howMany - 1)
if howMany > r then
rest <- randomPickIO' (howMany-1) tl (len - 1)
randomPickIO' howMany tl (len - 1)
This might contain some small parse errors or such (untested), but
could be okay.