Lesson 32 of 55
9 mins readPython The Either Type for Detailed Errors
In Plain English
While Maybe represents failure as Nothing, Either carries an error description on failure: Left error or Right successValue (conventionally "Right" is "correct").
Deep Dive: How It Works
Definition: data Either a b = Left a | Right b.
Convention: Left represents error information (e.g. String error message); Right represents successful computation value.
either Function: either onError onSuccess eitherVal unifies both branches into a final result.
Core Rules to Remember

Left = Error, Right = Success: Standard convention across all functional programming languages.

Rich Error Context: Pass structured error types or strings explaining why a computation failed.
Live Interactive Example
Hit Run Code to see it liveValidation with Either
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: validateAge returns Right on success and Left with descriptive error reason on failure.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Parse Positive Integer with Either
Define `checkPositive n = if n > 0 then Right n else Left "Must be positive"`.
Print `"Validated: "` followed by `show (checkPositive 100)`.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.