Theory

Algebraic Data Types (ADTs) build complex data from sums and products.

Expressiveness

Tuples, lists, Maybe, Either, user treesโ€”all ADTs.

Example

data Result a
  = Success a
  | Failure String

Note

Pattern matching + compiler exhaustiveness = strong invariants.

Implementation

Define once, then use in pattern matches, derive instances:

data Color = Red | Green | Blue
  deriving (Eq, Show)

Deriving saves boilerplate for Eq, Ord, Read, Show, etc.