Haskell/Understanding monads/Solutions/IO

Print a list of arbitrary values
The function  would print out every value from the list, but would produce. We'd thus need to chain, which is exactly how the function   is defined. Because we are not collecting the results of  but focus only on its actions, we can discard them with.

Generalize the bunny invasion example
If we were not working with monads, applying function  three times would be achieved by , which can be rewritten as. This suggests that if we want to generalize it to apply the function  times we could fold the list of  s using the   as the accumulating function: The initial value for  is   since $$f^0(x) = x$$.

Now all we have to do is translate this to monads, that is replace  with monad composition operator ,   with   and arbitrary   with  , yielding:

We can verify that it works as expected:

What is the expected behavior of for the   monad?
The  monad represents a possible failure. If a failure occurred at any point in computing a list then the whole list cannot be computed, thus we expect that  of a list will produce   if there are  s in the list and   a list otherwise. This is indeed what happens: