【发布时间】: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)有效。我希望有人能解释为什么它不等同于您的$版本!我猜类型注释的范围是语法。 -
嗨让-巴蒂斯特,谢谢,是的,工作,塔马斯