I never got a deep understanding of monads -- just a rough idea. I think, LispyScript starts helping me to get into monads. From reading the code in
I learnt that the macro doMonad transforms any code of the form
(doMonad <monad>
(<var1> <val1>
<var2> <val2>)
<expr>)
into
(mBind <val1>
(function (<var1>)
(mBind <val2>
(function (<var2>)
(mResult <expr>) | mZero ))))
For the sake of brevity I limited the number of bindings to two. The last line of the resolved expression indicates that mZero is being used as a value if possible; otherwise mResult is applied on <expr>.
With this understanding the identityMonad
(macro identityMonad ()
(object
"mBind" (function (mv mf) (mf mv))
"mResult" (function (v) v)))
makes perfect sense. The following interaction
lispy> (doMonad identityMonad (a 1 b (* a 2)) (+ a b))
3
corresponds to
lispy> ((function (a) ((function (b) ((function (v) v) (+ a b))) (* a 2))) 1)
3
Two questions:
Writing mondas seems to be hard, even somewhat counterintuitive. How does one come up with writing the code for the stateMonad or the arrayMonad? Is there any trick?
Can anybody help me formulating the monadic laws in LispyScript?
Any help is appreciated! It's really fun reading LispyScript code to learn monads!
Dominikus