Theory
Algebraic Data Types (ADTs) build complex data from sums and products.
- ๐ฆ Product Types combine fields (logical and).
- ๐ Sum Types choose between constructors (logical or).
- ๐ณ Recursive Types reference themselves.
- ๐ Polymorphic Data abstracts over element types.
Expressiveness
Tuples, lists, Maybe, Either, user treesโall ADTs.
Example
data Result a
= Success a
| Failure StringNote
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.