
If you look at this stuff long enough it almost begins to make sense. Maybe. ;-) I've been messing around with MonadPlus and I understand its usage with the Maybe and List monads. Since one use of Monads is combining computations, how can I combine a Maybe with a List? let m1 = Nothing let m2 = [1] let m3 = m1 `mplus` m2 ==> [1] --if the Maybe is Nothing, do nothing 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? Michael

michael rice wrote:
If you look at this stuff long enough it almost begins to make sense. Maybe. ;-)
I've been messing around with MonadPlus and I understand its usage with the Maybe and List monads. Since one use of Monads is combining computations, how can I combine a Maybe with a List?
let m1 = Nothing let m2 = [1] let m3 = m1 `mplus` m2 ==> [1] --if the Maybe is Nothing, do nothing
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?
Michael
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Hi Michael, You'll want the Data.Maybe.listToMaybe and Data.Maybe.maybeToList functions. -- Tony Morris http://tmorris.net/

Hi, normally, one uses monads to express and combine computations in the same monad. However, you can convert between some monads, e.g. from Maybe to List: import Data.Maybe (maybeToList)
let m1 = Nothing let m2 = [1] let m3 = maybeToList m1 `mplus` m2
let m1 = Just 1 let m2 = [] let m3 = maybeToList m1 `mplus` m2
In fact, you can convert from Maybe to any MonadPlus. maybeToMonadPlus Nothing = mzero maybeToMonadPlus (Just x) = return x And you can convert from List to any MonadPlus: listToMonadPlus Nothing = [] listToMonadPlus (x : xs) = return x `mplus` listToMonadPlus xs Now you should be able to do: m1 = maybeToMonadPlus (Just 1) m2 = listtoMonadPlus [2, 3] m3 = m1 `mplus` m2 :: Just Int -- Just 1 m4 = m1 `mplus` m2 :: [Int] -- [1, 2, 3] The reason this is possible is that Maybe and List do not support additional effects beyond what is common to all MonadPlus instances. Another option is to never specify which monad your computations are in in the first place. Instead, only specify which computational effects the monad should support. m1 = mzero :: MonadPlus m => m a m2 = return 1 :: (Monad m, Num a) => m a m3 = m1 `mplus` m2 `mplus` Just 2 -- Just 1 m4 = m1 `mplus` m2 `mplus` [2, 3] -- [1, 2, 3] In this version, m1 and m2 are polymorphic computations, which can be used together with List computations, Maybe computations, or any other MonadPlus instances. m1 needs MonadPlus, while m2 is happy with any Monad instance. This fact is encoded in their type. Tillmann
participants (3)
-
michael rice
-
Tillmann Rendel
-
Tony Morris