【问题标题】:Haskell: difference between let x = fn(intStr); return x and return fn(inpStr)?Haskell:let x = fn(intStr); 之间的区别返回 x 并返回 fn(inpStr)?
【发布时间】: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 xreturn fn(inpStr) 有什么区别?

【问题讨论】:

标签: haskell monads


【解决方案1】:

return fn(inpStr)return fn insStr 相同。你可能想要return (fn insStr)

在 Haskell 中,函数应用没有括号,并列就足够了。括号用于对事物进行分组。

【讨论】:

    猜你喜欢
    • 2022-08-03
    • 2021-02-02
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多