Haskell standard function (or simple composition) for "mjoin"?

This seems long, but lately I have needed the following:

mjoin :: (Monoid b, Monad m) => m b -> m b -> m b
mjoin a b = do
  a' <- a
  b' <- b
  return $ mappend a' b'

The example uses the following:

> mjoin (Just [1,2,3]) (Just [4, 5, 6])
Just [1,2,3,4,5,6]
> mjoin (Just [1,2,3]) Nothing
Nothing
> mjoin Nothing (Just [4, 5, 6])
Nothing

In other words, if both parameters Nothing, return Nothing. Otherwise, return the Justadded values.

Is there a standard function for this or a simpler formulation, possibly with >>=?

+4
source share
1 answer

Maybe something like this:

mjoin :: (Monoid b, Monad m) => m b -> m b -> m b
mjoin = liftM2 mappend
+9
source

All Articles