ユーザーが数値を入力し続け、負の数値を入力すると平均を出力するプログラムを作成しようとしてい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.