# :: (a -> b -> Maybe a) -> a -> [b] -> Maybe a

The foldM function is analogous to foldl, except that its result is encapsulated in a monad. Note that foldM works from left-to-right over the list arguments. This could be an issue where (>>) and the `folded function' are not commutative.
```foldM f a1 [x1, x2, ..., xm]

==

do
a2 <- f a1 x1
a3 <- f a2 x2
...
f am xm
```
If right-to-left evaluation is required, the input list should be reversed. Note: foldM is the same as foldlM
Left-to-right monadic fold over the elements of a structure. Given a structure t with elements (a, b, ..., w, x, y), the result of a fold with an operator function f is equivalent to:
```foldlM f z t = do
aa <- f z a
bb <- f aa b
...
xx <- f ww x
yy <- f xx y
return yy -- Just @return z@ when the structure is empty
```
For a Monad m, given two functions f1 :: a -> m b and f2 :: b -> m c, their Kleisli composition (f1 >=> f2) :: a -> m c is defined by:
```(f1 >=> f2) a = f1 a >>= f2
```
Another way of thinking about foldlM is that it amounts to an application to z of a Kleisli composition:
```foldlM f z t =
flip f a >=> flip f b >=> ... >=> flip f x >=> flip f y \$ z
```
The monadic effects of foldlM are sequenced from left to right. If at some step the bind operator (>>=) short-circuits (as with, e.g., mzero in a MonadPlus), the evaluated effects will be from an initial segment of the element sequence. If you want to evaluate the monadic effects in right-to-left order, or perhaps be able to short-circuit after processing a tail of the sequence of elements, you'll need to use foldrM instead. If the monadic effects don't short-circuit, the outermost application of f is to the rightmost element y, so that, ignoring effects, the result looks like a left fold:
```((((z `f` a) `f` b) ... `f` w) `f` x) `f` y
```

#### Examples

Basic usage:
```>>> let f a e = do { print e ; return \$ e : a }

>>> foldlM f [] [0..3]
0
1
2
3
[3,2,1,0]
```
Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.
O(n) Monadic fold with strict accumulator.
O(n) Monadic fold with strict accumulator