-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Soar with Haskell
By :

A second use case of ADTs is often called record or struct types. The purpose of records is to group or structure several related pieces of data.
We create the Person
ADT as a first simple example of a record datatype:
data Person = MkPerson String Int
Just like in the previous section, a new ADT is announced by the data
keyword, followed by the name of the new type, which is Person
in this case. Whereas the enumeration examples were defined in terms of a number of alternatives separated by |
characters, a record type has only one alternative. This alternative is the MkPerson
data constructor. What’s new is that this constructor takes two parameters, also called fields, of the String
and Int
types, respectively.
We create a new Person
by calling the constructor on values of the appropriate type:
tom :: Person tom = MkPerson "Tom" 45
In fact, the constructor behaves essentially like a function:
*Main> :t MkPerson MkPerson :: String...