[Haskell-cafe] Monadic Composition

Jorge Adriano Aires jadrian at mat.uc.pt
Wed May 12 02:33:40 EDT 2004

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 

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?

More information about the Haskell-Cafe mailing list