5 ms·
As a strong Haskell programmer I'm curious about the statement that "eliminating mutable state is neither possible nor desirable". My first response is to say t
by tel 14y ago
As a strong Haskell programmer I'm curious about the statement that "eliminating mutable state is neither possible nor desirable". My first response is to say that I find it both possible and desirable. My second is to note that "elimination of mutable state" isn't really the goal.
The real goal is to give mutable state a very composable, repeatable, clear, and restricted semantics. This is what occurs in the State monad in Haskell, for instance. You retain mutability (observable w.r.t. the stored state over applications) but can slice up the mutation and the intermediate, immutable values and recompose them in various forms with impunity.
That kind of state model is incredible to learn. Monads might be scary because they're unfamiliar, but in a functional context they can do some beautiful things.
- deleted 14y ago[deleted]
- tel 14y agoMonads are a way of reifying and manipulating context. If that context includes a background mutable state model, then yes. Monads are more useful than just for providing mutable state. That said, your example is just a constant function, not a monad at all. The point of monads wrt. mutable state is not that they hide it—indeed they uncover it and make it more explicit. I have to exactly specify where my code makes mutable or world-observable changes. Furthermore, through explicitness I can now manipulate, compose, adjust these abilities very flexibly.
- runT1ME 14y agoThe State Monad isn't really mutating state though, it's really a monad for composition of incremental operations upon immutable data structures. The ST monad is for 'hiding' mutable variables and ensuring their state doesn't violate referential transparency.
- tel 14y agoFrom inside the State monad though, it "looks just like" (has semantics of) mutability. It's simply interpreted in an immutable lambda calculus. The ST monad has the same game but a different interpreter.
- michaelochurch 14y agoAs a strong Haskell programmer I'm curious about the statement that "eliminating mutable state is neither possible nor desirable". My first response is to say that I find it both possible and desirable. It depends on your tools and your performance requirements. I guess I should say that "eliminating mutable state from your repertoire is neither possible or desirable". There are certainly many problems where you can. I agree strongly with everything else you said.
- tel 14y agoOh! Agreed completely now—there are times and algorithms that are definitely best done mutably. (Another plug for the ST monad, I suppose, since it can make it clear when you're doing such a thing.)