4 ms·
The writer monad gives you purely functional "printing".
by tuukkah 12y ago
The writer monad gives you purely functional "printing".
- mhluongo 12y agoI thought monads were going to be the answer at the end of the post... -_-
- wereHamster 12y agoThe IO monad gives you purely functional "printing". The Monad gives you purely functional sideeffects.
- ufo 12y agoI don't think the article really is about printing. He just uses printing as an analogy to point out that you can calculate changes without altering the simulation data during the calculations. And changing topic, I am not a huge fan of how the Writer forces your code into a monadic style. One of the things I enjoy the most when I program in Ocaml instead of Haskell is being able to put printfs or mutable buffers wherever I want.
- dllthomas 12y agoWriter isn't really about printing. It's about emitting values, to be later collected. That could very easily be state changes.
- ufo 12y agoI know. But even then, I kind of like being able to append values to a mutable buffer that is in scope without having to rewrite everything to use monads.
- dllthomas 12y ago"I know." Then I don't understand your earlier comment. "But even then, I kind of like being able to append values to a mutable buffer that is in scope without having to rewrite everything to use monads." We can agree to disagree, there.
- ufo 12y agoWhat I was trying to say is that I think there is some virtue in being able to add effects to your program without having to change the syntax you use. Its nice to only have to use List.map and not have two incompatible map and mapM functions. This is not to say that I don't value extra static safety. I think there is definitely room for languages that track effects without the syntactic weight of monads and monad transformers.
- dllthomas 12y agoAh, that's the bit I understood, and about which I said we could agree to disagree. To me, that reads like "I want to add new side effects and not document them," but I think coding style comes into play in complicated ways and it's not something I am eager to dig into right here, right now. What I did not understand was your response of "the article wasn't talking about actually printing" to the suggestion of using Writer, despite your knowing that Writer is not about actually printing, Writer being in fact a good fit for what was described in the article, and the poster having quoted "printing" in the first place.
- cronin101 12y ago> One of the things I enjoy the most when I program in Ocaml instead of Haskell is being able to put printfs or mutable buffers wherever I want. FYI, for the first need: http://hackage.haskell.org/package/base-4.8.0.0/docs/Debug-Trace.html http://hackage.haskell.org/package/base-4.8.0.0/docs/Debug-T...