Mutable data, that is, data that has a value that you can change over time as your program is running, is an idea that comes up more or less frequently in most programming languages. Mutability can allow you to write algorithms that are more efficient or more clear than their equivalents built without mutable data, but this mutability comes at a high cost, introducing a potential for any number of bugs that can cause programs to crash, or worse, to continue running while behaving incorrectly. Haskell, as a pure functional programming language, avoids mutability by default, but in some cases you’ll find that considered use of mutability can improve your programs.
When you decide to use mutable data in your Haskell program, you have a choice between several different approaches. Throughout the rest of this chapter you’ll see some motivating examples for when mutability can help you write functional programs, see some pitfalls to avoid when dealing with mutability, and learn how to use some of the different tools Haskell gives you for creating and working with mutable values.