Monads/Writer monad: Difference between revisions

Content added Content deleted
Line 429: Line 429:
unit(x, logmsg) = Writer(x, logmsg)
unit(x, logmsg) = Writer(x, logmsg)


bind(f, fmsg, w) = unit(f(w.x), fmsg)
bind(f, fmsg, w) = unit(f(w.x), w.msg * ", " * fmsg)


f1(x) = 7x
f1(x) = 7x
Line 435: Line 435:


a = unit(3, "after intialization")
a = unit(3, "after intialization")
b = bind(f1, " after times 7 ", a)
b = bind(f1, "after times 7 ", a)
c = bind(f2, " after plus 8", b)
c = bind(f2, "after plus 8", b)


println("$a => $b => $c")
println("$a => $b => $c")
</lang>{{out}}
</lang>{{out}}
<pre>
<pre>
after intialization: 3 => after times 7 : 21 => after plus 8: 29
after intialization: 3 => after intialization, after times 7: 21 => after intialization, after times 7, after plus 8: 29
</pre>
</pre>



=={{header|Kotlin}}==
=={{header|Kotlin}}==