Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell带IO monad错误,从IO到基本类型_Haskell - Fatal编程技术网

Haskell带IO monad错误,从IO到基本类型

Haskell带IO monad错误,从IO到基本类型,haskell,Haskell,我有这个功能: 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

我有这个功能:

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

liftM fsum vals
具有类型
IO Float
。您使用
let res=…
为它命名,但后来尝试将其用作
浮点值。您应该使用
liftM-fsum-vals
has-type
IO-Float
绑定它,根据需要生成一个
Float
。您使用
let res=…
为它命名,但后来尝试将其用作
浮点值。您应该绑定它,根据需要使用
生成
浮点值,GHC根据
putStrLn
推断
res
必须是
字符串

putStrLn res
更改为
putStrLn$show res


(ehird说的也是真的。)

GHC从
putStrLn
推断
res
必须是
字符串

putStrLn res
更改为
putStrLn$show res


(ehird说的也是真的。)

是的,这就是我编辑答案的原因。仅仅进行更改也不起作用:“无法将预期的类型
字符串
与推断的类型
浮点
//在
putStrLn
的第一个参数中,即
res
”噢,对不起;我误解了你的回答。是的,你完全正确。是的,这就是为什么我编辑了我的答案。仅仅进行更改也不起作用:“无法将预期的类型
字符串
与推断的类型
浮点
//在
putStrLn
的第一个参数中,即
res
”噢,对不起;我误解了你的回答。是的,你完全正确。
res <- liftM fsum vals
let res = fsum vals