3 ms·
The purpose of functional programming isn't to eradicate mutable state as if it were criminal. Mutable state is necessary. It's to control mutable state so that
by michaelochurch 16y ago
The purpose of functional programming isn't to eradicate mutable state as if it were criminal. Mutable state is necessary. It's to control mutable state so that it's easy to determine where state is and how it is expected to behave. The problem with state is that if the architecture is sloppy and it's all over the place, it can lead to impossible-to-reason-about program behavior, especially when the rules that govern the state have been written emergently by several parties over years. Mutable state is a flower in the right part of the garden and a weed in the wrong part of it.
Haskell takes the most extreme approach of the mainstream languages, which is to segregate mutable state entirely into monads (e.g. IO, STM). Ocaml does this in a more practical way: some types (e.g. arrays, hashtables, records with mutable fields, ref cells) are mutable while others (e.g. linked lists, tree-based associative maps) are constitutionally immutable. Common Lisp really isn't "purely functional" at all; it provides the tools necessary to write functional code, but it also has methods like NCONC and RPLACA which allow in-place modification of cons cells, put into the language for historical reasons.