Konrad Hinsen <[EMAIL PROTECTED]> wrote: > I have been following the recent "Monad tutorial" discussion with > interest, and even read the tutorial, which is a useful addition to > the existing Haskell documentation. So useful in fact that it raises a > question... > > The whole monad mechanism seems to geared towards functions of one > argument, plus eventually state, that get chained together. How about > functions with several arguments? > > 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. Many complicated proposals have been made, but there's a straightforward, general mechanism: > addMaybe :: Num alpha => Maybe alpha -> Maybe alpha -> Maybe alpha > addMaybe a b = a >>= \x -> > b >>= \y -> > return (x + y) or > addMaybe a b = do > x <- a > y <- b > return (x + y) (Incidentally, monads in Haskell are specified slightly differently than the Categorical version to enable precisely this sort of pattern.) Jon Cast _______________________________________________ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe