What I collect from your signature:
iterateM :: (Monad m) => (a -> ma) -> a -> [ma]
Is the nth element of iterateM fx
action that runs f
n
times. This is very close to iterate
, I suspect that we can implement it in terms of this.
iterate :: (b -> b) -> b -> [b]
iterate
gives us a list of b
s, and we need a list of ma
s, so I suspect b = ma
.
iterate :: (ma -> ma) -> ma -> [ma]
Now we need a way to convert f :: a -> ma
into something like ma -> ma
. Fortunately, this is exactly the definition of bind:
(=<<) :: (Monad m) => (a -> mb) -> (ma -> mb)
So:
\f -> iterate (f =<<) :: (a -> ma) -> ma -> [ma]
And to get the initial x :: a
to the desired ma
, we can use return
:
return :: (Monad m) => a -> ma
So:
iterateM fx = iterate (f =<<) (return x)
Explain to taste.
source share