[Haskell-cafe] How do I do conditional tail recursion in a monad?
David F. Place
d at vidplace.com
Wed Mar 21 08:31:10 EDT 2007
So, the next question is: Why isn't this already in Control.Monad?
On Mar 21, 2007, at 8:27 AM, Jules Bean wrote:
> David F. Place wrote:
>> Interesting, but what if 'p' is also a monadic action? For
>> instance, it might access the state of the State monad which 'f'
>> is updating.
>
> Then I'd stop trying to do it as a one-liner, I suspect:
>
> let untilM p f x = do
> cond <- p x
> if cond then return x
> else do y <- f x
> untilM p f y
>
>
>
>>
>> On Mar 21, 2007, at 5:31 AM, Jules Bean wrote:
>>
>>> ..but here 'f' is a pure function, not a monadic action. If you
>>> want f to be a monadic action then you want:
>>>
>>> *Main> :t let untilM p f x = if p x then return x else untilM p f
>>> =<< f x in untilM
>>> let untilM p f x = if p x then return x else untilM p f =<< f x
>>> in untilM :: (Monad m) => (a -> Bool) -> (a -> m a) -> a -> m a
>>
>> ___________________
>> (---o-------o-o-o---o-o-o----(
>> David F. Place
>> mailto:d at vidplace.com
>>
>>
>
___________________
(---o-------o-o-o---o-o-o----(
David F. Place
mailto:d at vidplace.com
More information about the Haskell-Cafe
mailing list