haskellhaskell-prelude

Why do I get an error when trying to `read` this value?


The .hs code :

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

Compilation :

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

While testing immediately after compilation:

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

Please guide. Why this error is occurring and how to avoid such errors?


Solution

  • lastName \"Diamond\"
    

    is missing an equals sign.

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