1 Introduction to functional programming
This chapter covers
- Understanding functional programming
- Thinking about intent instead of algorithm steps
- Understanding pure functions
- Benefits of functional programming
- C++’s evolution into a functional programming language
As programmers, we’re required to learn more than a few programming languages during our lifetime, and we usually end up focusing on two or three that we’re most comfortable with. It’s common to hear somebody say that learning a new programming language is easy—that the differences between languages are mainly in the syntax, and that most languages provide roughly the same features. If we know C++, it should be easy to learn Java or C#, and vice versa.
This claim does have some merit. But when learning a new language, we usually end up trying to simulate the style of programming we used in the previous language. When I first worked with a functional programming language at my university, I began by learning how to use its features to simulate for
and while
loops and if-then-else
branching. That was the approach most of us took, just to be able to pass the exam and never look back.