Haskell: let x = fn(intStr); 的区别return x 和 return fn(inpStr)?

Haskell: difference between let x = fn(intStr); return x and return fn(inpStr)?

在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 xreturn fn(inpStr)有什么区别?

return fn(inpStr) 等同于 return fn insStr。你可能想要 return (fn insStr).

在Haskell中函数应用没有括号,并列即可。括号用于对事物进行分组。