Hi all:
I am a newbie of haskell and I'm reading "
All about Monads" 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