1 What is functional programming?

This chapter covers

  • Understanding side effects and the problems they pose
  • Achieving a functional solution by removing side effects
  • Defining what a pure function is
  • Proving referential transparency and purity using the substitution model

Most of us started programming using an imperative style of coding. What do we mean by this? It means we give the computer a set of instructions or commands, one after the other. As we do so, we are changing the system’s state with each step we take. We are naturally drawn to this approach because of its initial simplicity. On the other hand, as programs grow in size and become more complicated, this seeming simplicity will lead to the very opposite; complexity arises and takes the place of what we initially intended to do. The end result is code that is not maintainable, difficult to test, hard to reason about, and (possibly worst of all) full of bugs. The initial velocity that we could deliver features slows down substantially until even a simple enhancement to our program becomes a slow and laborious task.

1.1 The benefits of FP: A simple example

1.1.1 A program with side effects

1.1.2 A functional solution: Removing the side effects

1.2 Exactly what is a (pure) function?

1.3 RT, purity, and the substitution model

1.4 What lies ahead