[Haskell-cafe] Combining computations

Luke Palmer lrpalmer at gmail.com
Sun May 3 06:41:20 EDT 2009


mplus requires both arguments to be in the same monad (the same type,
even).   Fortunately, the empty list behaves like Nothing, and a singleton
list behaves like Just.  So convert the Maybe before composing, using:

maybeToList Nothing = []
maybeToList (Just x) = [x]

(The maybeToList function can be found in Data.Maybe)

Keep in mind that this will give you:

Just 1 `mplus` [2,3,4]  ==>  [1,2,3,4]

Which may not be what you want...

Luke

On Sat, May 2, 2009 at 9:26 PM, michael rice <nowgate at yahoo.com> wrote:

> 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]
>
> If not, can someone supply a simple example of combining computations?
>
> Michael
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090503/7e19f5d1/attachment.htm


More information about the Haskell-Cafe mailing list