The easiest way to create a custom data type in Haskell is to use the data
keyword:
data Foo = Bar | Biz
The name of the type is specified between data
and =
, and is called a type constructor. After =
we specify all value constructors of our data type, delimited by the |
sign. There is a rule in Haskell that all type and value constructors must begin with a capital letter. The above declaration can be read as follows:
Define a type called
Foo
, which has two possible values:Bar
andBiz
.
let x = Bar
The above statement creates a variable named x
of type Foo
. Let's verify this by checking its type.
:t x
prints
x :: Foo