I see how the Writer monad allows one to produce a list of strings along with a set of computations, but what is the use of this? It seems very crude. There seem to be few examples of 'real world' use of Writer. What is this monad actually useful for, apart from didactic purposes?
Lack of examples seems to be one of the barriers to learning Haskell. It would be good if there was a comprehensive Haskell Cookbook to help. (So, is there such?)
For real world use, you can see that it is being used in these projects:
and many more...
Once you start writing more code, you will find out that Writer monad or it's transformer version is indeed useful in a lot of real world scenarios.
Writer
in itself is IMO rather useless. Yes, "logging in an otherwise-pure computation" makes sense, but for a simple pure computation you'd have to rewrite everything to use monadic binds. The result won't be dramatically nicer than if you had just rewritten it to manually concatenate log information.
What's really useful however is to add a WriterT
-layer to a monad stack you have anyway, so you get logging capability with almost no change to the definitions. This is what's done in "big powerful" monads like those used in Yesod and the like, but a WriterT
can just as well be applied to any simple special-purpose monad you use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With