Lesson 33 of 55
9 mins readPython Pattern Matching on Maybe & Either
In Plain English
Learn idiomatic ways to extract wrapped values from Maybe and Either using pattern matching, case expressions, and the catMaybes utility.
Deep Dive: How It Works
Pattern Matching Maybe: handle (Just x) = process x; handle Nothing = fallback.
catMaybes: Filters a list [Maybe a] -> [a], dropping all Nothing elements and keeping only unwrapped Just values.
isJust & isNothing: Boolean query functions in Data.Maybe.
Core Rules to Remember

Exhaustive Unpacking: Pattern matching guarantees all potential failure cases are covered.

catMaybes Filtering: Extract valid values from a collection of optional results cleanly.
Live Interactive Example
Hit Run Code to see it liveFiltering and Processing Maybe Collections
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: catMaybes stripped all Nothing elements safely.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Extract with fromMaybe
Use `fromMaybe 100 (Just 50)` and assign to `val`.
Print `"Extracted: "` followed by `show val`.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.