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

Jürgen Doser jurgen.doser at gmail.com
Fri Sep 17 13:27:02 EDT 2010


El sáb, 18-09-2010 a las 00:48 +0800, ender escribió:
> 
> 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
> 
The composition of these functions would then be nice, yes. However, you
would have to duplicate the ugly handling of Nothings in each of the
functions father, mother, sister, brother, etc.:

father Nothing = Nothing
father (Just x) = ...

And you would still need a function of type Sheep -> Maybe Sheep, to
fill in the dots.

> 
> 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.

Look at the the type m a -> (a -> m b) -> m b in a slightly different
way, namely by flipping its arguments. You get:

(a -> m b) -> (m a -> m b)

i.e., it transforms a function of type a -> m b into one of type m a ->
m b, which can then be composed by (.). Given that we want to be able to
deal with function of type a -> m b, This is exactly what we want.
The usually given type m a -> (a -> m b) -> m b is just a convention
because of the closer correspondence to the do-notation.

	Jürgen



More information about the Haskell-Cafe mailing list