
Hello, I needed to compose monadic functions, since I couldn't find anything in Control.Monad so I defined my own. I first came up with a left associative version: compLM [f1,..., fn] a = (f1 a >>= f2) >>= ... >>= fn
compLM :: Monad m => [a->m a] -> (a->m a) compLM mfs a = foldl (>>=) (return a) mfs
Later a right associative version, which I believe to be more efficient, particularly when some computation returns fail: compRM [f1,..., fn] a = \x->f1 x >>= (\x->f2 x >>= ... >>= \x->fn x) a
compRM :: Monad m => [a->m a] -> (a->m a) compRM = foldr (\f g-> (\x ->f x >>= g)) return
This higher order function seems quite useful to me and not trivial, so I expected it to be available. When this happens I wonder if I'm missing something. Am I complicating things? Is it actually available (or some other that does the trick) ? Or is it just me who finds this usefull enough to be in the libs? J.A.

On Wed, May 12, 2004 at 01:33:40AM +0100, Jorge Adriano Aires wrote:
This higher order function seems quite useful to me and not trivial, so I expected it to be available. When this happens I wonder if I'm missing something. Am I complicating things? Is it actually available (or some other that does the trick) ? Or is it just me who finds this usefull enough to be in the libs?
I've used such a function at least once, in context of processing program options (search for foldl in http://www.haskell.org/pipermail/haskell/2004-January/013412.html). So, yes, it is useful, but should it be included in a standard Monad module? After all, this module contains mostly trivial functions ;) BTW. You can write this function using foldM: compM l a = foldM (#) a l where # is an often used reverse application operator: x # f = f x Best regards, Tom -- .signature: Too many levels of symbolic links

So, yes, it is useful, but should it be included in a standard Monad module? After all, this module contains mostly trivial functions ;)
BTW. You can write this function using foldM:
compM l a = foldM (#) a l
where # is an often used reverse application operator: x # f = f x
Right. Now that I look at it, someone probably tried to give me this advice before but I failed to understand... (sorry monotonom!). It's all clear now. One more question. Isn't the foldM description a bit misleading? From the Report and also in GHC documentation: "The foldM function is analogous to foldl, except that its result is encapsulated in a monad.(...) foldM f a1 [x1, x2, ..., xm ] == do a2 <- f a1 x1 a3 <- f a2 x2 ... f am xm" After reading this I expected "left associativity", just like in my first definition. That'd mean a fail wouldn't stop a computation immediately but instead be passed from function to function. By checking its definition in the report I can see this is not the case though. Cheers, J.A.
participants (2)
-
Jorge Adriano Aires
-
Tomasz Zielonka