【问题标题】:Couldn't match expected type Person with actual type IO () [closed]无法将预期类型 Person 与实际类型 IO () 匹配 [关闭]
【发布时间】:2017-02-12 12:51:29
【问题描述】:

我已经开始学习 Haskell,并阅读 Learn You Haskell。第 8 章处理“制作我们自己的类型和类型类”,我有一条错误消息,这对我来说是个问题。可能解决方案是一件小事,但我找不到它,所以请透露一下提示或帮助我解释一下。

data Person = Person { firstName :: String  
                     , lastName :: String  
                     , age :: Int  
                     } deriving (Eq, Show, Read)  

mikeD = Person {firstName = "Michael", lastName = "Diamond", age = 43}  

main = print $ read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" :: Person

这是我收到的错误消息

Couldn't match expected type `Person' with actual type `IO ()'
In a stmt of a 'do' block:
    print
    $ read
        "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" ::
      Person
In the expression:
  do { print
       $ read
           "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" ::
         Person }
In an equation for `main':
    main
      = do { print
             $ read
                 "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" ::
               Person }

提前致谢:)

【问题讨论】:

  • 尝试在 read 语句周围添加括号。
  • print (read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" :: Person) 有效。我希望有人能解释为什么它不等同于您的$ 版本!我猜类型注释的范围是语法。
  • 嗨让-巴蒂斯特,谢谢,是的,工作,塔马斯

标签: haskell types io


【解决方案1】:
main = 
  print $ read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" :: Person

被解析为

main = 
  (print $ read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}") :: Person

你的意思是

main = 
  print (read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" :: Person )

【讨论】:

  • 你好 Zeta,谢谢你的评论,例子很清楚,Tamas
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
相关资源
最近更新 更多