[Haskell-cafe] Combining computations

Kalman Noel noel.kalman at googlemail.com
Sun May 3 05:17:15 EDT 2009


michael rice schrieb:
> let m1 = Just 1 
> let m2 = []
> let m3 = m1 `mplus` m2  ==> [1]  --if the Maybe is not Nothing, add it to the list
> 
> Or am I misunderstanding combining computations?

You just got the type of mplus wrong:

	mplus :: (MonadPlus m) => m a -> m a -> m a

Note that it takes two values of the same type (m a), but you're giving
it values of different types.  That is, combining computations of
different types is not within the scope of MonadPlus.  In this case, it
makes sense to convert (Just 1) to [1] via Data.Maybe.maybeToList, thus:

	m1 = Just 1
	m2 = [2,3]
	m3 = maybeToList m1 `mplus` m2   -- [1,2,3]

Note also that, in this example, Monoid (mappend) instead of MonadPlus
(mplus) would be sufficient.  Actually MonadPlus becomes useful only
when you are concerned about the extra properties that its instances are
expected to satisfy.

Kalman


More information about the Haskell-Cafe mailing list