Monads and Maybe
Wed, 20 Aug 2003 06:45:35 -0400
On Tue, 19 Aug 2003 14:09:16 +0100 (BST)
C T McBride <email@example.com> wrote:
> > > As an example, I'll use the Maybe monad. Suppose I want to write
> > > code to handle experimental data, in which there might be missing
> > > values. I might then decide to represent measurements by data of
> > > type "Maybe Double", with missing values represented by "Nothing".
> > > I could then go on to define functions on missing values, which
> > > would return "Nothing" when their argument is "Nothing", and I
> > > could string these functions together via the monad mechanism.
> > > Fine. But how would I handle e.g. addition of two such values?
> > > The result should be "Nothing" when either of its arguments
> > > is"Nothing". Is there any mechanism to handle that?
> > Yes, liftM2. Defined in module Monad (or Data.Monad resp.).
> > > Konrad.
> > Wolfgang
> Or, more generally,
> infixl 9 <$>
> (<$>) :: Monad m => m (s -> t) -> m s -> m t
> mf <$> ms =
> do f <- mf
> s <- ms
> return (f s)
or just liftM2 ($)
or just ap