Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Writer monad

Tags:

haskell

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?)

like image 994
andro Avatar asked Dec 26 '14 00:12

andro


2 Answers

For real world use, you can see that it is being used in these projects:

  • Yesod
  • Persistent
  • hs-webdriver
  • Yaml
  • Esqueleto
  • hspec

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.

like image 114
Sibi Avatar answered Nov 02 '22 19:11

Sibi


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.

like image 38
leftaroundabout Avatar answered Nov 02 '22 21:11

leftaroundabout