【问题标题】:How to return a tuple in Haskell如何在 Haskell 中返回一个元组
【发布时间】:2016-08-28 10:12:44
【问题描述】:
type Record=([Char],[Char],[Char],[Char],[Char])

createAccount::IO()->Record
createAccount=do
    putStrLn "Enter First Name"
    fname<-getLine
    putStrLn "Enter Last Name"
    lname<-getLine
    putStrLn "Enter State"
    state<-getLine
    putStrLn "Enter City"
    city<-getLine
    putStrLn "Enter House No."
    hnum<-getLine
    let hnumInt = read hnum :: Integer
    putStrLn "Enter Contact"
    contact<-getLine
    return (fname,lname,state,city,contact)

【问题讨论】:

  • 在显式类型绑定中出现此 errorType 错误 *** 术语:createAccount *** 类型:IO ([Char],[Char],[Char],[Char],[Char]) * ** 不匹配:IO () -> 记录
  • 似乎没有任何理由得到门牌号; createAccount 不用。

标签: haskell tuples


【解决方案1】:

错误信息是createAccount的类型签名实际上是:

createAccount :: IO ([Char],[Char],[Char],[Char],[Char])

不是你所说的IO() -&gt; Record

修复它的选项:

  • 将类型签名更改为IO ([Char],[Char],[Char],[Char],[Char])(或等效:IO Record
  • 删除类型签名 - 您不需要它,因为 Haskell 可以解决。

【讨论】:

  • 移除类型签名 - 你不需要它,因为 Haskell 可以解决。 不过,将类型签名添加到所有顶级绑定是一种很好的做法。跨度>
猜你喜欢
  • 2014-04-02
  • 2018-01-31
  • 1970-01-01
  • 2011-03-05
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
相关资源
最近更新 更多