[Haskell-cafe] Feeding a monad into itself
ky3 at atamo.com
Fri Jul 21 15:57:08 UTC 2017
Another perfectly cromulent definition is:
untilNothing f = fromJust . last . takeWhile isJust . iterate (f =<<) . Just
This has 2 advantages:
1. It illustrates the haskellism that "A list is a loop is a list."
2. It composes much-beloved list combinators into a reasonable pipeline.
On Fri, Jul 21, 2017 at 12:26 AM, Jake <jake.waksbaum at gmail.com> wrote:
> I have a function f :: a -> Maybe a that I want to feed into itself with
> some initial input until it returns Nothing.
> untilNothing :: (a -> Maybe a) -> a -> a
> untilNothing f x = case f x of
> Just x' -> untilNothing f x'
> Nothing -> x
> Is there a better way to do this? I feel like there is something more
> general going on with Monads being fed into themselves, but maybe I'm
> wrong. Thoughts?
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe