[Haskell-cafe] Combining computations
daniel.is.fischer at web.de
Sun May 3 07:19:30 EDT 2009
Am Sonntag 03 Mai 2009 05:26:22 schrieb michael rice:
> I posted something similar about an hour ago but it seems to have gotten
> lost. Very strange.
> I've read that Monads can combine computations. Can a Maybe monad be
> combined with a List monad such that
> Nothing `mplus`  ==> 
> Just 1 `mplus`  ==> 
Not directly, the type of mplus is
mplus :: MonadPlus m => m a -> m a -> m a
, so the monad has to be the same for both arguments. For  and Maybe, you can use
maybeToList and listToMaybe to convert one into the other:
Prelude Data.Maybe Control.Monad> maybeToList Nothing
Prelude Data.Maybe Control.Monad> maybeToList (Just 1)
Prelude Data.Maybe Control.Monad> maybeToList Nothing `mplus` 
Prelude Data.Maybe Control.Monad> maybeToList (Just 1) `mplus` 
Prelude Data.Maybe Control.Monad> Nothing `mplus` listToMaybe 
Prelude Data.Maybe Control.Monad> Nothing `mplus` listToMaybe [1,2,3]
, for certain other combinations, you can also have a meaningful conversion from one monad
to another (e.g.
stateToStateT :: Monad m => State s a -> StateT s m a
stateToStateT comp = StateT (return . runState comp)
) and employ the technique to combine them, but it's of limited use.
A monad allows you to combine computations of 'similar' type (for some fuzzy meaning of
similar), using (>>=), (>>) to combine them sequentially and perhaps mplus to combine them
> If not, can someone supply a simple example of combining computations?
More information about the Haskell-Cafe