【发布时间】:2016-04-01 01:47:34
【问题描述】:
在 Haskell 中,我有一个函数
fn :: String -> Int
然后我尝试在 IO monad 中使用它
mn = do
inpStr <- readFile "input.txt"
return fn(inpStr)
编辑:我正在使用 GHCi,版本 7.10.2
我收到了错误消息
Couldn't match type ‘[Char] -> Integer’ with ‘IO b’
Expected type: String -> IO b
Actual type: String -> [Char] -> Integer
Relevant bindings include mn :: IO b (bound at 01.hs:13:1)
The function ‘return’ is applied to two arguments,
but its type ‘([Char] -> Integer) -> String -> [Char] -> Integer’
has only three
In a stmt of a 'do' block: return fn (inpStr)
In the expression:
do { inpStr <- readFile "input.txt";
return fn (inpStr) }
但是,如果我将代码更改为
mn = do
inpStr <- readFile "input.txt"
let x = fn(inpStr)
return x
有效。
let x = fn(intStr); return x 和 return fn(inpStr) 有什么区别?
【问题讨论】: