[Haskell-cafe] example in "All about Monads"
James Andrew Cook
mokus at deepbondi.net
Mon Sep 20 08:47:40 EDT 2010
Also keep in mind that "Maybe" is a sort of a toy example of the concept being developed. It is indeed useful as a monad in its own right, but certainly not a case where the "comb" function is absolutely essential, as wren demonstrated. There are stronger motivations for using the 'comb' approach in cases where the type in place of 'Maybe' is more complex, or even completely abstract so you can't match against its constructors directly.
On Sep 17, 2010, at 11:26 PM, wren ng thornton wrote:
> On 9/17/10 12:48 PM, ender wrote:
>> my question is, why not define the function father and mother as type of
>> father::Maybe Sheep -> Maybe Sheep? this can also
>> clean the code and it avoid the additional function comb
> Do note that `comb` is giving you a natural way of creating those functions:
> flip comb :: (a -> Maybe a) -> (Maybe a -> Maybe a)
> Since we can define comb, every (a -> Maybe a) has an associated function of type (Maybe a -> Maybe a). And if we use comb, then that means we can use the function at either type, just pick whichever one is easier for us to use at the time. If we only ever defined (Maybe a -> Maybe a) functions then we'd need to use a combinator to go the other way...
> f :: a -> Maybe a
> g :: Maybe a -> Maybe a
> f = ...
> g = flip comb f
> -- or --
> f = g . Just
> g = ...
> Whether we use (flip comb) or (. Just) doesn't really matter too much. The point is that we can go both ways.
> Live well,
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe