2017-02-12 142 views
1

我已經開始學習Haskell,並正在閱讀Learn You Haskell。第8章涉及「創建我們自己的類型和類型類」,並且我有一個錯誤消息,這對我來說是一個問題。可能解決方案是一件小事,但我找不到它,所以請揭示這個提示,或者幫我解釋一下。無法匹配預期類型實際類型爲IO的人()

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 } 

感謝提前:)

+4

嘗試在read語句周圍添加parens。 – Reactormonk

+0

'print(閱讀「Person {firstName = \」Michael \「,lastName = \」Diamond \「,age = 43}」:: Person)'的作品。我希望有人會解釋爲什麼它不等於你的'''版本!我猜測類型註釋的範圍是句法。 –

+0

嗨讓巴蒂斯特,謝謝,是的,作品,塔馬斯 – bling5630

回答

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) 
+0

你好澤塔,謝謝你的評論,這個例子很清楚,塔馬斯 – bling5630

相關問題