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

Brandon Allbery allbery.b at gmail.com
Sat Feb 23 01:42:06 CET 2013


On Fri, Feb 22, 2013 at 7:27 PM, xiao Ling <lingxiao at seas.upenn.edu> wrote:

> h :: M Int -> M Int -> M Int
> h x y = bind ( \x-> g x y ) x
>
> where g is
>
> g :: Int -> W Int -> W Int
> g x y = y >>= (return . (+x))
>
> for the monad:
>
> data M a = M a deriving Show
>
> 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?
>
Because it's a different "x".  Lemme rewrite it slightly:

h :: M Int -> M Int -> M Int
h x y = bind ( \w -> g w y ) x

All I did was replace the inner "x" with "w", to demonstrate that it has no
relationship to the outer "x"; the \... -> syntax introduces new local
bindings unrelated to any outside of it, in this case for "w" (or what he
had "x", shadowing the original binding of "x" within the lambda).

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130222/8a72a12e/attachment.htm>


More information about the Beginners mailing list