Lesson 29 of 55
10 mins readAlgebraic Data Types (data ADT)
In Plain English
Algebraic Data Types (ADTs) allow defining rich custom domain types. Combine alternative variants (sum types with |) and composite fields (product types).
Deep Dive: How It Works
Sum Types (Enums/Variants): data TrafficLight = Red | Yellow | Green deriving (Show, Eq).
Product Types with Arguments: data Shape = Circle Double | Rectangle Double Double.
Automatic Deriving: deriving (Show, Eq, Ord) instructs GHC to generate standard typeclass instances automatically.
Core Rules to Remember

Sum & Product Types: Model domain states with mathematical precision.

deriving (Show, Eq): Automatically generate string representation and equality comparison.
Live Interactive Example
Hit Run Code to see it liveGeometric Shapes ADT
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: area uses pattern matching to deconstruct Circle and Rectangle constructors.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Define Direction ADT
Define `data Direction = North | South | East | West deriving (Show, Eq)`.
Print `"Direction: "` followed by `show North`.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.