在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)
?
return fn(inpStr)
是一样的return fn insStr
.你可能想要的return (fn insStr)
.
在Haskell中没有函数应用的括号,并置就足够了.括号用于分组事物.