Assume we want to create a data type Person, which has a first and last name, an age, a phone number, a street, a zip code and a town.
We could write
data Person = Person String String Int Int String String String
If we want now to get the phone number, we need to make a function
getPhone :: Person -> Int
getPhone (Person _ _ _ phone _ _ _) = phone
Well, this is no fun. We can do better using parameters:
data Person' = Person' { firstName :: String
, lastName :: String
, age :: Int
, phone :: Int
, street :: String
, code :: String
, town :: String }
Now we get the function phone
where
:t phone
phone :: Person' -> Int
We can now do whatever we want, eg:
printPhone :: Person' -> IO ()
printPhone = putStrLn . show . phone
We can also bind the phone number by Pattern Matching:
getPhone' :: Person' -> Int
getPhone' (Person {phone = p}) = p
For easy use of the parameters see RecordWildCards