A monad is a container that carries a value together with rules for what happens next. It makes function pipelines safer: instead of scattering null checks, exceptions, and effectful operations through every caller, each step passes along either a usable value or a controlled outcome. The goal is practical fault-tolerant composition, not category theory. section 5.0 section 5.5
Functors can map a function over a wrapped value, but composing functions that each return wrappers can produce nested containers. A monad adds operations such as join or chain to flatten that structure and continue with the contained value. chain is map followed by flattening: use it when the next function already returns a monad. section 5.3 section 5.4
Common monadic containers represent different kinds of context:
- Maybe represents a value that may be absent. Just holds a value; Nothing propagates absence, so later map calls are skipped. getOrElse supplies a fallback at the boundary. section 5.3
- Either represents success or failure in one return type. Right carries a usable result, while Left carries an error or failure value and bypasses ordinary transformations. section 5.3
- IO represents a delayed effect. Mapping builds a description of reading or writing, and run performs the stored effect explicitly. section 5.3
Safe student lookup pipeline
A student-display pipeline can start by wrapping an SSN, clean the input, validate its length, find the student, select fields, convert them to CSV, and append the result to the page. Use chain for validation and lookup because they return monads; use map for ordinary transformations. If validation or lookup fails, dependent steps are skipped, and an error handler or fallback can handle the outcome. section 5.4 section 5.4
Figure 5.11. Same two functions as in figure 5.10; but this time the null value travels in a monad (Either or Maybe), which causes the rest of the functions in the pipeline to gracefully fail.
Compare an unsafe composition with one where a missing value travels inside Maybe or Either, allowing the remaining pipeline to fail gracefully. section 5.4
Use monads when values can be missing, operations can fail, or effects need an explicit boundary. By keeping those conditions in containers with a shared interface, pipelines stay fluent while error-handling behavior remains local to the container type. section 5.4 section 5.5





