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` [] ==> [1]
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) [1] Prelude Data.Maybe Control.Monad> maybeToList Nothing `mplus` [1] [1] Prelude Data.Maybe Control.Monad> maybeToList (Just 1) `mplus` [] [1] Prelude Data.Maybe Control.Monad> Nothing `mplus` listToMaybe [1] Just 1 Prelude Data.Maybe Control.Monad> Nothing `mplus` listToMaybe [1,2,3] Just 1 , 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 'in parallel'. > > If not, can someone supply a simple example of combining computations? > > Michael _______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
