There's a great tutorial here that seems to suggest to me that the Writer Monad is basically a special case tuple object that does operations on behalf of (A,B). The writer accumulates values on the left (which is A) and that A has a corresponding Monoid with it (hence it can accumulate or mutate state). If A is a collection, then it accumulates.
The State Monad is also a object that deals with an internal tuple. They both can be flatMap'd, map'd, etc. And the operations seem the same to me. How are they different? (please respond back with a scala example, I'm not familiar with Haskel). Thanks!
Your intuition that these two monads are closely related is exactly right. The difference is that
Writer
is much more limited, in that it doesn't allow you to read the accumulated state (until you cash out at the end). The only thing you can do with the state in aWriter
is tack more stuff onto the end.More concisely,
State[S, A]
is a kind of wrapper forS => (S, A)
, whileWriter[W, A]
is a wrapper for(W, A)
.Consider the following usage of
Writer
:Now we can run the computation:
We could do exactly the same thing with
State
:And then:
But this is a little more verbose, and we could also do lots of other things with
State
that we couldn't do withWriter
.So the moral of the story is that you should use
Writer
whenever you can—your solution will be cleaner, more concise, and you'll get the satisfaction of having used the appropriate abstraction.Very often
Writer
won't give you all the power you need, though, and in those casesState
will be waiting for you.