Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Getting a value out of an RVar in Data.Random

How do I write a function with a type signature like:

mySample :: StdGen -> Int -> [a] -> [a]

in terms of

sample :: Int -> [a] -> RVar [a]

where sample is defined in Data.Random.Extras? Here, sample is a function which returns a random sublist from the given list.

like image 359
emchristiansen Avatar asked Jan 25 '12 20:01

emchristiansen


1 Answers

According to the documentation, this should work:

mySample :: StdGen -> Int -> [a] -> [a]
mySample g n xs = fst $ sampleState (sample n xs) g

However, I get overlapping instance errors when trying to compile it. I got this to compile, though:

mySample :: StdGen -> Int -> [a] -> [a]
mySample g n xs = evalState (runRVar (sample n xs) StdRandom) g
like image 67
hammar Avatar answered Oct 21 '22 10:10

hammar