klequis / zz-haskell-notebook

Notes from learning Haskell
1 stars 0 forks source link

Data constructors #40

Open klequis opened 2 years ago

klequis commented 2 years ago

Data constructors in Haskell provide a means of creating values that inhabit a given type. Data constructors in Haskell have a type and can either be constant values (nullary) or take one or more arguments, like functions. In the following example, Cat is a nullary data constructor for Pet, and Dog is a data constructor that takes an argument:

-- Why name a cat?
-- They don't answer anyway.
type Name = String
data Pet = Cat | Dog Name

The data constructors have the following types:

Prelude> :t Cat
Cat :: Pet
Prelude> :t Dog
Dog :: Name -> Pet