Banner B. Schafer.

Monad Combinators

Complete reference for Haskell Monad, Traversable, and Foldable operations

Functor & Applicative

fmap / (<$>)
(a → b) → f a → f b
Map function over structure
(+1) <$> Just 5
pure
a → f a
Lift value into context
(<*>)
f (a → b) → f a → f b
Apply wrapped function
(*>)
f a → f b → f b
Sequence, keep second
(<*)
f a → f b → f a
Sequence, keep first
liftA2
(a → b → c) → f a → f b → f c
Lift binary function

Core Monad

return
a → m a
Inject value (= pure)
(>>=)
m a → (a → m b) → m b
Bind: sequence with passing
Just 5 >>= \x → Just (x+1)
(>>)
m a → m b → m b
Sequence, discard first
(=<<)
(a → m b) → m a → m b
Reverse bind

Monad Operations

(>=>)
(a → m b) → (b → m c) → a → m c
Kleisli composition L-to-R
(<=<)
(b → m c) → (a → m b) → a → m c
Kleisli composition R-to-L
join
m (m a) → m a
Flatten nested monads
void
f a → f ()
Discard result

Traversable

traverse
(a → f b) → t a → f (t b)
Map with applicative effects
sequenceA
t (f a) → f (t a)
Flip structure inside-out
sequenceA [Just 1, Just 2]
= Just [1,2]
mapM
(a → m b) → t a → m (t b)
traverse specialized to Monad
sequence
t (m a) → m (t a)
sequenceA specialized to Monad
forM
t a → (a → m b) → m (t b)
Flipped mapM
mapM_
(a → m b) → t a → m ()
Map action, discard results
forM_
t a → (a → m b) → m ()
Flipped mapM_
traverse_
(a → f b) → t a → f ()
Traverse for side effects
sequenceA_
t (f a) → f ()
Sequence for side effects

Foldable

foldr
(a → b → b) → b → t a → b
Right-associative fold
foldr (+) 0 [1,2,3] = 6
foldl
(b → a → b) → b → t a → b
Left-associative fold
foldl’
(b → a → b) → b → t a → b
Strict left fold
foldMap
Monoid m => (a → m) → t a → m
Map to monoid then fold
null
t a → Bool
Test if structure is empty
length
t a → Int
Count elements
elem
Eq a => a → t a → Bool
Check if element present
maximum
Ord a => t a → a
Find maximum element
minimum
Ord a => t a → a
Find minimum element
sum
Num a => t a → a
Sum all elements
product
Num a => t a → a
Product of all elements
toList
t a → [a]
Convert structure to list

Alternative & MonadPlus

empty / mzero
f a / m a
Represents failure
(<|>) / mplus
f a → f a → f a
Choice/combine operation
guard
Bool → m ()
Conditional failure
mfilter
(a → Bool) → m a → m a
Filter by predicate
💡 Key Patterns
Sequencing: Use >>= for dependent computations | Traversal: Use traverse/mapM to collect results, traverse_/mapM_ for side effects | Folding: Use foldr for lazy folds, foldl’ for strict folds | Choice: Use <|> for fallback