[Haskell-cafe] example in "All about Monads"

ender crazyender at gmail.com
Fri Sep 17 12:48:32 EDT 2010


Hi all:
     I am a newbie of haskell and I'm reading "All about
Monads<http://www.haskell.org/all_about_monads/html/>"
right now, I have some question about the example shows in Chapter 2, it
says that following code is ugly


mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = case (mother s) of
                                 Nothing -> Nothing
                                 Just m  -> case (father m) of
                                              Nothing -> Nothing
                                              Just gf -> father gf


and after intruduce the comb function, it become more cleaner:

-- comb is a combinator for sequencing operations that return Maybe comb ::
Maybe a -> (a -> Maybe b) -> Maybe b comb Nothing _ = Nothing comb (Just x)
f = f x -- now we can use `comb` to build complicated sequences
mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = (Just s) `comb` mother `comb` father `comb`
father

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

further more, why we need a function => with type of => m a -> ( a -> m b )
-> mb? define some function with type m a -> m b can solve these problems
too.


Thanks and BR
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100917/e117bdc6/attachment.html


More information about the Haskell-Cafe mailing list