Theory
A recursive type contains values of itselfโessential for trees, linked lists, ASTs.
data Tree a
= Empty
| Node a (Tree a) (Tree a)Induction Principle
Define functions by:
- Base case โ value built with base constructor(s).
- Inductive case โ assume function defined for subโstructures, build for whole.
Implementation
Example: Height
height :: Tree a -> Int
height Empty = 0
height (Node _ l r) = 1 + max (height l) (height r)Recursion mirrors data shape, ensuring termination when structure is finite.