On day 5, I wrote about how to make a set monad — first build a sequence of instructions that describe how to build a Set, and then interpret that sequence of instructions to give a result, an actual Set.
Set
Maybe is like [] but “smaller” — Maybe t can store zero or one elements of type t, but not more.
Maybe
[]
Maybe t
t
There’s a category theory way of relating Maybe and []: natural transformations convert between Functors while also making sure…
Functor
Free monads let you take a data type that represents instructions, or (approximately the same) effects that you want to happen, and then have interpreters which understand how to make those effects actually happen.
A continuation is “the rest of the program” represented as a value that you can do value things to: stick it in a variable, store it in a data structure, pass it around to other functions, as well as (if you want) actually cause it to run.
In the Reader monad, all you can do is query the environment by asking for the whole environment. If you only want a little bit of that environment, you can apply a function to the environment. For example, you might ask for the…
Reader
ask
Sets are a bit like lists, and so you can probably imagine that there should be a Monad instance for them. The first part of this post will be why that does not work in the “obvious” way, and the second part will describe a different way that does work.
Monad
Here’s a list comprehension:
> [ x * y | x <- [1,2,3], y <- [10,100]][10,100,20,200,30,300]
This works in do notation too, with a bit of rearranging:
do
Prelude> doPrelude| x…
Lazy evaluation lets us compose two different computations without thinking too much about who is “in control”: we can generate primes lazily, and take the root of them lazily and evaluation alternates back and…
The list monad lets you perform non-deterministic searches for a solution to a problem. At each step of your program, you can provide a choice of possible values to bind to a variable, and the rest of the program will run several times, trying each possible value and…
Reader, Writer and State usually come presented together, because they’re very similar: these monads wire together functions by adding in extra parameters or return values. There is no non-determinism, change in control flow, or…