1

.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

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

4

1 回答 1

7
lastName \"Diamond\"

缺少一个等号。

read "Person {firstName = \"Michael\", lastName = \"Diamond\", age = 43}" :: Person
于 2012-07-23T11:58:01.553 回答