Prev: Appendix I - A physical analogy for monads | TOC: Contents |

This appendix contains a list of all of the code examples supplied with the tutorial.

This example is discussed in the section: Meet the monads.

The example code introduces the monad concept without using Haskell typeclasses. It shows how a monadic combinator can be used to simplify the construction of computations from sequences of computations which may not return a result.

This example is discussed in the section: Doing it with class.

The example code builds on the first example, and shows how
do-notation can be used with an instance of the `Monad`

class
(in this case, `Maybe`

is the monad used).

This example is discussed in the section: Monad support in Haskell.

The example code builds on the first two examples, and shows a
somewhat atypical — but very powerful — use of
the `foldM`

function outside of a do-block.

This example is discussed in the section: Monad support in Haskell.

The example code shows a more typical use of the `foldM`

function
within a do-block. It combines dictionary values read from different files
into a single dictionary using the `foldM`

function within
the IO monad.

This example is discussed in the section: Monad support in Haskell.

The example code shows the use of the `filterM`

function
within a do-block. It prints the subset of its arguments that
specify directories and ignores non-directory arguments.

This example is discussed in the section: Monad support in Haskell.

The example code shows the use of the `liftM`

function
within a do-block. It looks up a name in a list and uses a lifted
String manipulation function to modify it within the Maybe monad.

This example is discussed in the section: Monad support in Haskell.

The example code shows a higher-order application of `liftM2`

.
It folds lifted operations within the List monad to produce lists
of all combinations of elements combined with the lifted operator.

This example is discussed in the section: Monad support in Haskell.

The example code shows a higher-order application of `ap`

.
It folds `ap`

through a list of `Maybe (a->a)`

functions to process sequences of commands.

This example is discussed in the section: Monad support in Haskell.

The example code shows the use of `msum`

in the Maybe monad
to select the first variable match in a stack of binding environments.

This example is discussed in the section: Monad support in Haskell.

The example code shows the use of `guard`

in the Maybe monad
to select only the records from a list that satisfy a predicate
(equivalent to `filter`

).

This example is discussed in the section: The Maybe monad.

The example code shows how to use the `Maybe`

monad
to build complex queries from simpler queries that may fail to
return a result. The specific example used is looking up
mail preferences for someone based on either their full name
or a nickname.

This example is discussed in the section: The Error monad.

The example code demonstrates the use of the `Either`

type constructor as an `Error`

monad with a custom
error type. The example parses hexadecimal digits and uses the
exception handling mechanism of the `Error`

monad to
provide informative error messages in the event of a parse failure.

This example is discussed in the section: The List monad.

The example code uses the built-in list type constructor as a monad for non-deterministic computation. The example demonstrates parsing an ambiguous grammar consisting of integers, hex values, and words.

This example is discussed in the section: The IO monad.

The example code implements a simple version of the standard
Unix command "tr". The example demonstrates use of the IO monad
including implicit `fail`

calls due to pattern matching
failures and the use of `catcherror`

.

This example is discussed in the section: The State monad.

The example code shows how the State monad can be used instead of explicitly passing state. The example uses the State monad to manage the random number generator state while building a compound data value requiring multiple calls to the random number generator.

This example is discussed in the section: The Reader monad.

The example code shows how the Reader monad can be used to simplify computations involving a shared environment. The example uses the Reader monad to implement a simple template substitution system. The example code demonstrates the use of the Parsec monadic parser combinator library.

This example is discussed in the section: The Writer monad.

The example code shows how the Writer monad can be used to implement logging. The example implements a very simple firewall simulator and uses the Writer monad to log the firewall activity.

This example is discussed in the section: The Continuation monad.

The example code shows how the Continuation monad's escape continuations work. The example computes a complex transformation of a number.

This example is discussed in the section: Combining monads the hard way.

The example code shows how the Continuation monad can be nested within the IO monad given a suitable computational structure. The example is a slight modification of example 18.

This example is discussed in the section: Combining monads the hard way.

The example code shows how the Continuation monad and IO monad can be used simultaneously, but without using monad transformers. The example builds on examples 18 and 19.

This example is discussed in the section: Monad transformers.

The example code shows how the transformer version of the Continuation monad can be used to create a combined monad for using continuations and doing I/O. The example builds on examples 18, 19 and 20.

This example is discussed in the section: Standard monad transformers.

The example code shows how the transformer version of the Writer monad can be used to create a combined monad for logging and doing I/O. The example adds timestamps to the log entries of the firewall simulator from example 17.

This example is discussed in the section: Standard monad transformers.

The example code shows how the transformer version of the Reader monad can be used to create a monad that combines a shared environment with I/O. The example converts the template system of example 16 to use files as templates.

This example is discussed in the section: Standard monad transformers.

The example code uses the `StateT`

transformer on the List
monad to create a combined monad for doing non-deterministic stateful
computations. The example uses the combined monad to solve a logic
problem.

This example is discussed in the section: An example with multiple monad transformers.

The example code uses the `StateT`

and `WriterT`

transformers on the List monad to create a combined monad for doing
non-deterministic stateful computations with logging. The example uses the
combined monad to solve the N-queens problem.

This example is discussed in the section: Heavy lifting.

The example code demonstrates the use of the `lift`

function
and the necessity of managing its use in complex transformer stacks.

Prev: Apendix I - A physical analogy for monads | TOC: Contents |