Monads/Writer monad: Difference between revisions
Content added Content deleted
(Add Factor) |
m (→{{header|Factor}}: bug fix rolled into release; workaround no longer required) |
||
Line 238: | Line 238: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2019-10-06}} |
|||
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. |
|||
Factor comes with an implementation of Haskell-style monads in the <code>monads</code> vocabulary. |
|||
<lang factor>USING: kernel math math.functions monads prettyprint |
<lang factor>USING: kernel math math.functions monads prettyprint ; |
||
FROM: monads => do ; |
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> ] |
[ 5 "Started with five, " <writer> ] |
||
[ sqrt "took square root, " <writer> ] |
[ sqrt "took square root, " <writer> ] |