我正在尝试编写一个程序,其中用户不断输入数字并在输入负数后输出平均值这是我试图保持pure separate from unpure
有人可以给我修复此代码的提示,以及纯和不纯的分离提示。
getFloat :: IO Float
getFloat = do line <- getLine
return (read line:: Float)
“不纯的部分”
average :: IO Float
average = do x <- getFloat
return((fst help x)/(snd help x))
“纯粹的部分”
help x
|x>=0 = (x+sum1, 1+ counter)
|otherwise = (sum1 , counter)
where
sum1 = 0.0
counter = 0.0
我收到这个错误
.hs:45:26:
Couldn't match expected type `(t0 -> Float, b0)'
with actual type `t1 -> (t1, t2)'
In the first argument of `fst', namely `help'
In the first argument of `(/)', namely `(fst help x)'
In the first argument of `return', namely
`((fst help x) / (snd help x))'
Failed, modules loaded: none.