September 2023
Intermediate to advanced
470 pages
11h 12m
English
The exercises in this chapter will focus on recursion and folding.
Here is your friendly reminder that nobody can learn functional programming by just reading code, and the best way to learn is by practicing. You can refer to the exercises folder in the book code repository for the starting point and some hints.
Let’s try to write a function in recursive style to investigate the Collatz conjecture.
We start with a sequence defined in this way: take a positive integer n>1; if it is even, divide it by 2; if it is odd, multiply it by 3 and add 1. Then repeat the process.
The conjecture says that no matter which number you start with, you’ll end with 1.
For example, starting with 6, we have this sequence: 6 -> 3 ...
Read now
Unlock full access