[Haskell-cafe] Feeding a monad into itself

David Menendez dave at zednenem.com
Fri Jul 21 04:51:45 UTC 2017


Another possibility is to use Either for an early exit.

iterateM :: Monad m => (a -> m a) -> a -> m b
iterateM f a = f a >>= iterateM f

exitOnNothing :: (a -> Maybe a) -> a -> Either a a
exitOnNothing f a = maybe (Left a) Right (f a)

Now, your untilNothing is iterateM . exitOnNothing, but you can easily
extend it to use IO or whatever pattern you like.


On Thu, Jul 20, 2017 at 11:17 PM, Jake <jake.waksbaum at gmail.com> wrote:
> That's what I was thinking, but I couldn't figure out if I wanted MonadPlus,
> Alternative or maybe Monoid because they all have zero elements.
>
>
> On Thu, Jul 20, 2017, 21:52 Joshua Grosso <jgrosso1 at asu.edu> wrote:
>>
>> Could MonadPlus (with mzero) or Alternative (with empty) provide the
>> termination condition, if this pattern turned out to be more generally
>> useful?
>>
>> Joshua Grosso
>>
>> On Thu, Jul 20, 2017 at 7:16 PM, Jeff Clites <jclites at mac.com> wrote:
>>>
>>> Also this pattern does seem Maybe-specific, in that for the general Monad
>>> case there's not an obvious termination condition.
>>>
>>> JEff
>>>
>>> On Jul 20, 2017, at 11:03 AM, Rein Henrichs <rein.henrichs at gmail.com>
>>> wrote:
>>>
>>> This is about as good as you can do. The monad-loops package provides a
>>> variety of similar combinators, generally implemented in similar ways, but
>>> not this specific combinator. The downside of this combinator is that it is
>>> partial: it will run forever without producing anything if f never gives a
>>> Nothing.
>>>
>>> On Thu, Jul 20, 2017 at 10:27 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:
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>>> Only members subscribed via the mailman list are allowed to post.
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> To (un)subscribe, modify options or view archives go to:
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>> Only members subscribed via the mailman list are allowed to post.
>>>
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> To (un)subscribe, modify options or view archives go to:
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>> Only members subscribed via the mailman list are allowed to post.
>>
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.



-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list