为什么在尝试“读取”此值时出现错误?

时间:2012-07-23 11:51:39

标签: haskell haskell-prelude

.hs代码:

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

编译:

*Main> :load "/home/optimight/baby.hs"  
[1 of 1] Compiling Main             ( /home/optimight/baby.hs, interpreted )  
Ok, modules loaded: Main.  

编译后立即测试:

*Main> read "Person {firstName = \"Michael\", lastName \"Diamond\", age = 43}" :: Person  
*** Exception: Prelude.read: no parse

请指导。为什么会出现此错误以及如何避免此类错误?

1 个答案:

答案 0 :(得分:7)

lastName \"Diamond\"

缺少等号。

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