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

2007-03-21 Thread Juan Carlos Arevalo Baeza
I suppose there are plenty of flavors for such functions, and they are simple enough to write. One I've been using a bit is this one: loopM :: Monad m => a -> (a -> m (Maybe a)) -> m () loopM start action = loop start where loop i = do result <- action i case

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

2007-03-21 Thread Dominic Steinitz
These sort of things come up from time to time. Why not make a proposal? http://www.haskell.org/pipermail/haskell-cafe/2006-February/014214.html Dominic. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/ha

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

2007-03-21 Thread David F. Place
On Mar 21, 2007, at 8:40 AM, Jules Bean wrote: Part of the reason is all the possible variations (monadic action, monadic test, monadic filter, etc etc), and it's all really very easy to write yourself. Indeed, I had just written whileM_ p a = do cond <- p ; if cond then do a ; whileM_ p

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

2007-03-21 Thread Jules Bean
David F. Place wrote: So, the next question is: Why isn't this already in Control.Monad? Some people have proposed it. Part of the reason is all the possible variations (monadic action, monadic test, monadic filter, etc etc), and it's all really very easy to write yourself. Perhaps it's e

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

2007-03-21 Thread David F. Place
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 t

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

2007-03-21 Thread Jules Bean
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

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

2007-03-21 Thread David F. Place
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. 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

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

2007-03-21 Thread Jules Bean
DavidA wrote: So I figure untilM should look something like: untilM :: Monad m => (a -> Bool) -> (a -> m a) -> a -> m a untilM p f x = return (if p x then x else untilM p f (f x)) The problem is that the two branches of the conditional have different types. If I try to remedy that by changing "t

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

2007-03-21 Thread DavidA
Hi, I would like to write a function untilM, which would be to until as mapM is to map. An example use would be if I had a function dice :: State Int Int which returns random dice throws within a state monad. Then I'd like to be able to do something like untilM (\(s,p)-> s>=100) f (0,1) whe