The ST
monad is exactly for the safe embedding of mutable operations within pure code. The type system is leveraged to ensure that none of the mutated data can escape the scope, thus you get the power of local mutable state without the peril of making your entire program stateful (which could destroy referential transparency or introduce race conditions).
Some documentation on the ST monad:
- Haddock
- Haskell Wiki
- ST to quick sort vectors (stack overflow) - see the function named
vsort
. - The original paper