Monads/Writer monad: Difference between revisions
Content added Content deleted
(Add Jsish) |
(Add Factor) |
||
Line 236: | Line 236: | ||
half → 1 |
half → 1 |
||
</lang> |
</lang> |
||
=={{header|Factor}}== |
|||
Factor comes with an implementation of Haskell-style monads in the <code>monads</code> vocabulary. Due to what may be a bug, the log reads backward. This can be rectified by redefining the <code>writer</code> method for <code>>>=</code> to say <code>prepend</code> instead of <code>append</code>. I will run this by the language maintainers and hopefully the redefinition can be removed in the future. |
|||
<lang factor>USING: kernel math math.functions monads prettyprint sequences ; |
|||
FROM: monads => do ; |
|||
M: writer >>= |
|||
[ run-writer ] curry |
|||
[ dip [ run-writer ] compose dip prepend <writer> ] curry ;</lang> |
|||
Now we can get on with it: |
|||
<lang factor>{ |
|||
[ 5 "Started with five, " <writer> ] |
|||
[ sqrt "took square root, " <writer> ] |
|||
[ 1 + "added one, " <writer> ] |
|||
[ 2 / "divided by two." <writer> ] |
|||
} do .</lang> |
|||
{{out}} |
|||
<pre> |
|||
T{ writer |
|||
{ value 1.618033988749895 } |
|||
{ log |
|||
"Started with five, took square root, added one, divided by two." |
|||
} |
|||
} |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |