Recursion, Control, and Higher-Order Functions

Pattern matching gives us decision making, but what about loops and so on? Most of the time, we don’t really need them either.

Again, think data and transformations. When we have a collection of things to process in some way—say, a list of numbers that we want to turn into a list of colors, or a tree containing strings for which we want the leaf count—most of the transformations we want fall into two main patterns: mapping and folding.

Mapping is about applying the same operation to everything in a collection, but keeping the shape the same. For example, we can add 2 onto elements in the list [1,2,3] to get [3,4,5]. The order and count of elements stays the same. The operation to perform is ...

Get Functional Programming: A PragPub Anthology now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.