Whenever I read about Monad example, they always present IO as a case study.
Are there any examples of monads doing list manipulation which somebody could present? I aprpeciate this could be overkill, but I am interested if monads can present advantages over regular list manipulation techniques.
The big secret to the list monad in Haskell is that list comprehensions are syntactic sugar for do blocks. Any time you write a list comprehension, you could have written it using a do block instead, which uses the list monad instance.
A simple example
Let’s say you want to take two lists, and return their cartesian product (that is, the list of
(x,y)for every combination ofxfrom the first list andyfrom the second list).You can do that with a list comprehension:
The list comprehension is syntactic sugar for this do block:
which in turn is syntactic sugar for
A more complicated example
That example doesn’t really demonstrate the power of monads, though, because you could easily write it without relying on the fact that lists have a Monad instance. For example, if we only use the Applicative instance:
Now let’s say you take every element of a list of positive integers, and replicate it as many times as itself (so e.g.
f [1,2,3] = [1,2,2,3,3,3]for example). Who knows why you’d want to do that, but it is easy:That’s just syntactic sugar for this:
which in turn is syntactic sugar for
This time we can’t write that just using the applicative instance. The key difference is that we took the output from the first bind (
x) and made the rest of the block depend on it (y <- replicate x x).