无法匹配预期类型Person与实际类型IO()

时间:2017-02-12 12:51:30

标签: haskell types io

我已经开始学习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 }

提前致谢:)

1 个答案:

答案 0 :(得分:5)

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 )