Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

This seems a long shot, but recently I've had a need for 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 use is this:

> 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 either parameters are Nothing, then return Nothing. Else, return Just and the appended values.

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

like image 810
Ana Avatar asked Jan 09 '23 07:01

Ana


1 Answers

Perhaps something like this:

mjoin :: (Monoid b, Monad m) => m b -> m b -> m b
mjoin = liftM2 mappend
like image 143
sabauma Avatar answered Jan 28 '23 21:01

sabauma