[Haskell-beginners] Beginners Digest, Vol 56, Issue 33

Brent Yorgey byorgey at seas.upenn.edu
Sat Feb 23 01:44:05 CET 2013

On Fri, Feb 22, 2013 at 07:27:28PM -0500, xiao Ling wrote:
> Hi All: How do you define a function of signature h :: M Int -> M Int -> M
> Int  so that h ( M x ) ( M y ) = M ( x + y ), but without unwrapping the
>  value from the monad?
>  This question is from the article "Trivial Monad" found at
> http://blog.sigfpe.com/2007/04/trivial-monad.html. The provided answer is
> h x y = x >>= (\x -> g x y)
> Now I am a little confused, how can you put x in g if it takes an Int as
> first parameter but x is M Int?

I think your confusion may stem from the fact that there are *two
different* things named 'x' in the above code.  The x's on the right
hand side of the >>= shadow the x on the left hand side.  This is
confusing and poor style; a better way to write it would be

  h x y = x >>= (\i -> g i y)

If you study the type of >>= you will see that i indeed has type Int,
as required for the first argument of g.


More information about the Beginners mailing list