[Haskell-cafe] MonadPlus

Dan Piponi dpiponi at gmail.com
Fri May 9 16:43:05 EDT 2008


Andrew asked,

> ...so it's a kind of choice operator? Run all actions until you get to one
> that succeeds and return the result from that?

The eternal bit of trickiness for Haskell is that type classes group
often together things that people don't immediately see as similar -
monads probably being the best example. So it's probably best not to
immediately try to find an all-encompassing summary of MonadPlus like
"a kind of choice operator" but instead concentrate on the definition.
The key thing is the line

> mplus :: m a -> m a -> m a

You can combine two m a's together with a binary operation to get
another m a. Absolutely any binary operation (with an identity) that
fits the rather lax laws that come with MonadPlus will do. Depending
on context mplus might mean "either/or", or "and", or "this first, or
else that" or something entirely different that nobody here has
anticipated yet. For each monad m, just try to think about how you
might combine a pair of m a's and what might serve as an identity.
--
Dan


More information about the Haskell-Cafe mailing list