[Haskell-cafe] name of this monadic combinator?

Jason Dagit dagit at codersbase.com
Sun May 30 04:46:38 EDT 2010


On Sun, May 30, 2010 at 1:35 AM, Michael Vanier <mvanier42 at gmail.com> wrote:

> I stumbled across this monadic combinator:
>
> mcombine :: Monad m => (a -> a -> a) -> m a -> m a -> m a
> mcombine f mx my = do
>    x <- mx
>    y <- my
>    return (f x y)
>
> I used it to chain the outputs of two Parsec String parsers together using
> this operator:
>
> (<++>) :: Monad m => m String -> m String -> m String
> (<++>) = mcombine (++)
>
> mcombine seems like such a basic operation that it should be a library
> function, but I couldn't find one matching it on hoogle.  Is there one?
>

It's a lift of sorts.  The tip off is the type signature.  Your type is
equivalent to this type:
Monad m => (a -> a -> a) -> (m a -> m a -> m a)

Hopefully the parens added for the grouping helps make the intuition clear.
 Your function takes a function and makes a new one that expects monadic
parameters.

Look at liftM2 in Control.Monad.

More generally there is a formula here:
liftM<N> f x1 .. x<N> = f` `liftM` x1 `ap` ... `ap` x<N>

If you have an applicative this could also be:
f <$> m1 <*> m2

HTH,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100530/2c0a81f2/attachment.html


More information about the Haskell-Cafe mailing list