

Translation from Haskell to JavaScript (Introduction to monads)  - khingebjerg
http://blog.jcoglan.com/2011/03/05/translation-from-haskell-to-javascript-of-selected-portions-of-the-best-introduction-to-monads-ive-ever-read/

======
tumult
In the author's own words: _I should stress that this is very hand-waving
imprecise definition..._

I don't want to be too hard on this guy, but this is a bad article. Sorry.
This article uses incorrect terms to define things. It also doesn't explain or
illustrate why monads are useful. You cannot read this article and learn
anything about monads.

It's not useful on a theoretics level, and it's also not useful on a practical
Haskell level. I don't think the author understands what he is writing about.
(Don't get me wrong, by the way – it's totally fine to write about something
you don't understand in order to learn more about it. But maybe don't publish
it prominently on your website and peddle it out to newbies as learning
material.)

The overabundance of bad monad articles is making it harder for Haskell
newbies to learn. There are many good, free resources for learning about
Haskell, such as _Real World Haskell_. This is not one of them.

 _Think of a monad as a spacesuite full of nuclear waste in the ocean next to
a container of apples. now, you can't put oranges in the space suite or the
nucelar waste falls in the ocean, BUT the apples are carried around anyway,
and you just take what you need._ \- Dons, talking about bad monad
explanations

edit: also, if you're really interested in seeing what a Haskell monad looks
like literally translated into JavaScript, I wrote a comment here on HN a year
ago with some code: <http://news.ycombinator.com/item?id=1275860> I could
leave better comments if I wrote it again today, but the code is correct and
works.

~~~
evanrmurphy
Hmm! That's funny, I thought I was "getting" monads better after reading this
article (as someone who doesn't already have a firm grasp of monads, of
course).

You see, I already sensed that monads:

\- were a way that you could sort of simulate side effects using pure
functions

\- had to do with the type signatures of functions

What I gleaned from the article is that monads:

\- have to do with making functions composable

\- work by way of the abstractions bind and unit in order to make type
signatures match

Did the article harm my (lack of) understanding of monads?

~~~
amalcon
Monads basically let you add an extra calculation to your functions. Why would
you want to do that? Mainly so that you don't have to do it manually. Think of
it as tacking an extra calculation onto the side of each function that
operates in the monad.

For a "state" monad, that calculation is maintaining/passing the state. For a
"logging" monad, that calculation is maintaining the log. For an "error
handling monad", that calculation is checking for and propagating errors.

~~~
evanrmurphy
I know lisp better than Haskell, and so I'm more familiar with macros than
with monads. From what you describe, it seems to me that the use cases for
monads and macros have some overlap.

Take the OP's example for the Writer monad, in which several functions needed
to return a debug string "<function name> was called" in addition to their
main return value. If I had these two functions+:

    
    
      (def sine (x)
        [(Math.sin x) "sine was called"])
    
      (def cube (x)
        [(* x x x) "cube was called"])
    

I might write a macro like the following (rather than a monad) to abstract
away their common pattern:

    
    
      (mac defWriter (name parms body...)
        `(def ,name ,parms
           [,@body (+ ,name " was called.")]))
    

And then each function's definition could be written more concisely:

    
    
      (defWriter sine (x)
        (Math.sin x))
    
      (defWriter cube (x)
        (* x x x))
    

\---

\+ Forgive me for indulging in the syntactic sugar from my own
lisp->javascript project in these examples, but since we're dealing in
JavaScript I couldn't resist. <https://github.com/evanrmurphy/lava-script>

------
joeyh
Anyone else disappointed that this was not about machine translation of
Haskell to Javascript, with some sort of DOM monad?

While Haskell -> Javascript has been done before, all the examples seem toys
without a rich integration into the browser.

