我有這樣的功能:哈斯克爾與IO單子錯誤,從IO基本型
onData :: IO ([Float]) -> IO()
onData vals =
do
let res = liftM fsum vals
putStrLn " * Processing ... "
putStrLn res
putStrLn " * Sum : "
putStrLn " * Done Processing"
return()
fsum :: [Float] -> Float
fsum [] = 0
fsum (x:xs) = x + fsum(xs)
而這個功能,我在「FSUM」呼叫得到一個錯誤。我錯過了什麼?我只想要返回的值。
HaskellParseData.hs:20:14:
Couldn't match expected type `[Char]' with actual type `IO Float'
Expected type: String
Actual type: IO Float
In the first argument of `putStrLn', namely `res'
In a stmt of a 'do' expression: putStrLn res
這將無法正常工作; 'IO Float'沒有'Show'實例。 – ehird 2012-02-06 22:22:22
是的,這就是我編輯我的答案的原因。只是做出你的改變也是行不通的:「無法匹配預期的類型'字符串'與推斷的類型'Float' //在'putStrLn'的第一個參數中,即'res'」 – 2012-02-06 22:24:46
哦,對不起;我誤解了你的答案。是的,你完全正確。 – ehird 2012-02-06 22:26:17