I am trying to generate a list of randomized boolean values based on a moving threshold.
This is my code. The function at issue is isActionRandom
:
randomBool :: Int -> IO Bool
randomBool p = do n <- randomRIO(1,100)
return (if n > p then True else False)
isActionRandom :: Float -> Int -> Int -> Int -> IO [Bool]
isActionRandom eps n m step = [ randomBool (floor (eps-(0.01*y) * 100))
| x <- [1..(n*m)]
, y <- floor(x/(m*step))]
isActionRandom
does not compile because the list created is type [IO Bool]
and not IO [Bool]
as I intended. How do I "extract" the boolean value from the IO function at each iteration, and build a list of the intended type?
You can use the sequence
method from Traversable
:
sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
In your case, it will have the type:
sequence :: [IO Bool] -> IO [Bool]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.