I need to convert a list of IO [Float]
to [Float]
. I get a IO [Float]
object from the following function:
probs :: Int -> IO [Float]
probs 0 = return []
probs n = do
p <- getStdRandom random
ps <- probs (n-1)
return (p:ps)
I understand that the result of this function is a list with type [IO Float] and not a list of numbers. It is a list of I/O actions that generate numbers. The I/O wasn't executed yet, so in my case the random number generator hasn't actually generated the numbers. What I want to do is generate each random number of the contents of this list, so I can get a [Float]
list.
I need this to calculate the amount of numbers in every quartile of the result (to check the distribution among random numbers):
calcQuartile :: [Float] -> [Float] -> [Int]
calcQuartile randomList (x1:x2:rest) = length(filter (\x -> x>=x1 && x<x2) randomList):calcQuartile randomList (x2:rest)
calcQuartile x y = []
I use the following code to run this function, which is not working:
calcQuartile (probs x) [0,0.25..1]
The error which I get is:
• Couldn't match expected type ‘[Float]’
with actual type ‘IO [Float]’
• In the first argument of ‘calcQuartile’, namely ‘(probs x)’
In the expression: calcQuartile (probs x) [0, 0.25 .. 1]
In an equation for ‘getAmountInQuartile’:
getAmountInQuartile x = calcQuartile (probs x) [0, 0.25 .. 1]