unfoldM is:exact

A monadic unfold. Subject to fusion Since 1.1.2
Unfold a cofree comonad from a seed, monadically.
Unfold a free monad from a seed, monadically.
Unfold a free monad from a seed, monadically.
The supplied Maybe expression will be repeatedly called until it returns Nothing. All values returned are collected into a list.
unfoldM f seed builds an InputStream from successively applying f to the seed value, continuing if f produces Just and halting on Nothing.
ghci> is <- Streams.unfoldM (n -> return $ if n < 3 then Just (n, n + 1) else Nothing) 0
ghci> Streams.toList is
[0,1,2]
O(n) The unfoldM function is analogous to the Stream unfoldr. unfoldM builds a ByteStream from a seed value. The function takes the element and returns Nothing if it is done producing the ByteStream or returns Just (a,b), in which case, a is a prepending to the ByteStream and b is used as the next element in a recursive call.
cycle ties a finite ByteStream into a circular one, or equivalently, the infinite repetition of the original ByteStream. | O(n) The unfoldM function is analogous to the Stream 'unfoldr'. unfoldM builds a ByteStream from a seed value. The function takes the element and returns Nothing if it is done producing the ByteStream or returns Just (a,b), in which case, a is a prepending to the ByteStream and b is used as the next element in a recursive call.
Construct by unfolding a monadic data structure This is the most memory-efficient way to construct ListT where the length depends on the inner monad.
This is unfoldM from monad-loops. It repeatedly runs an IO action until it return Nothing, and puts all the Justs in a list. If you find yourself using more functionality from monad-loops, just add the package dependency instead of copying more code from it.
A monadic unfold.
Create an Automaton from a state and an effectful step function.