[Haskell-beginners] Monads in javascript

Martin Drautzburg Martin.Drautzburg at web.de
Sat Jul 16 10:16:52 CEST 2011


Back to the question "why can't I get the intermediate results"

In Haskell I can do things like

f::Int->[Int]
f x = [x+1]

z :: [Int]
z = do 
  y1 <- [1,2,3]
  y2 <- f y1
  return (y1*y2)

i.e. I can access an intermediate result y1 and the end of the chain. When I 
write this without do notation I get

z' = [1,2,3] >>= \y1->[y1+1] >>= \y2->return (y1*y2)

which of course works just as well. However I must not put parentheses around 
the lambdas:

z' = [1,2,3] >>= (\y1->[y1+1]) >>= (\y2->return (y1*y2))
Not in scope: `y1'


However 
z' = [1,2,3] >>= (\y1->[y1+1] >>= (\y2->return (y1*y2)))

works again, which misled me to believe that (>>=) associates to the right.

In javascript a lambda would look like this

f = function(y1) {return [y1+1]}

I cannot see how I could possible write a function (>>=) which chains such 
lambdas such that I can still access the arguments outside the function 
bodies. It is like there are always parentheses around the lambdas. Well there 
actually are braces in javascript, but it can't be just the syntax.

So what is javascript missing? Is it because in haskell a lambda is just an 
expression wheras in javascript it is something special?




-- 
Martin



More information about the Beginners mailing list