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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.