[Haskell-cafe] How do I do conditional tail recursion in a monad?

Jules Bean jules at jellybean.co.uk
Wed Mar 21 08:27:39 EDT 2007


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
>
>



More information about the Haskell-Cafe mailing list