[Haskell-beginners] Monads in javascript

Martin Drautzburg Martin.Drautzburg at web.de
Tue Jul 12 23:27:42 CEST 2011


Hello all,

I found some implementations of Monads in javascript, but they all do not 
allow capturing the intermediate results. My question is: "what is the 
ultimate cause for this"?

In haskell the second argument to (>>=) is a function a->Mb which can be 
written as a lambda expression, e.g. 
        \x->[x]. 
In javascript such a function would be written as 
        function(x) {return[x]}.

Did I get this right: in haskell the chain of >>= is constructed from the end? 
So chaining a->Mb and b->Mc gives you a->Mc, which is again suitable as a 
second argument to (>>=), right?

So why can't I do this is javascipt? Or can I?

The reason I am asking this is because I am trying to beautify callbacks. An 
asynchronous ajax call needs a function argument, which will be executed once 
the call completes. But what if I want to take the results of the first call, 
do something with them and pass it to a second ajax call, where I would again 
have to pass another function argument. I would end up with a deeply nested 
structure of lambdas, something like

f(a,b,...function(...){
        ... 
        g(... function(...)

I had some hopes that chaining functions monad-style would ease my pain. I 
might be on the wrong track though, feel free to tell me so.

-- 
Martin

-- 
Martin



More information about the Beginners mailing list