I am trying to write a function that uses a random number as a condition to compare to a list (which is created by mapping a function over a range of integers). I'm doing it interactively, and if I do it by defining each term separately it works:
import System.Random.MWC (create)
import Statistics.Distribution (genContVar)
import Statistics.Distribution.Uniform (uniformDistr)
rng <- create
rd <- (genContVar (uniformDistr 0 1)) rng
f x = takeWhile (<rd) $ fmap (*x) [1..10]
alternatively, I can use a non-random Double with a let expression and also have no problem
f x = let rd = 0.4 in takeWhile (<rd) $ fmap (*x) [1..10]
however, if I try to put it all together I get an error
f x = let rand <- (genContVar (uniformDistr 0 1) g) in takeWhile (<rand) $ fmap (*x) [1..10]
<interactive>:39:16: error:
parse error on input ‘<-’
Perhaps this statement should be within a 'do' block?
I understand that having different variable types prevents so much as adding up and Int and a Double, and that monads are very particular, but being new to Haskell I'm hoping to avoid the broader philosophy about that for now and instead try to find a practical way of using random numbers in general functions.
Aucun commentaire:
Enregistrer un commentaire