|
|
|
|
|
by evanrmurphy
5577 days ago
|
|
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 |
|