Recursion
Recursive definitions, recursive programs and proofs about them
The topics of this chapter follow Kameron Decker Harris’s Fall 2026 lecture notes on recursion and recursive functions. These come from those notes:
- the two parts of a recursive definition, a base case and a recursive rule;
- the recursive definitions of the natural numbers and of the strings over an alphabet;
- the three questions to ask when writing a recursive function: the base case, the recursive call and the combination;
- the definition of a recursive algorithm.
Everything else is new: the exercises, the other examples, the Racket functions, the algorithm, the theorems and their proofs.
1 Recursive definitions
2 Recursively defined sets
3 Recursion in Racket
4 Lists
5 Recursive algorithms
6 Proofs about recursive definitions
Proof. We use induction (Definition: Proof by induction), following Remark: The shape of a proof by induction with \(n_0 = 0\). Let \(P(n)\) be the equation \(a_n = 2^n + 1\).
Base case. \(a_0 = 2 = 2^0 + 1\).
Inductive step. Let \(k \ge 0\) be an integer, and suppose \(P(k)\): \(a_k = 2^k + 1\). The goal \(P(k + 1)\) is \(a_{k + 1} = 2^{k + 1} + 1\). Since \(k + 1 \ge 1\), the recursive rule gives \(a_{k + 1}\), and
\[ \begin{aligned} a_{k + 1} &= 2 a_k - 1 && \text{(recursive rule)} \\&= 2 \mathopen{}\left(2^k + 1\right)\mathclose{} - 1 && \text{(induction hypothesis)} \\&= 2^{k + 1} + 2 - 1 \\&= 2^{k + 1} + 1. \end{aligned} \]
So \(P(k + 1)\) holds, and by induction \(a_n = 2^n + 1\) for every non-negative integer \(n\).
Proof. Let \(M\) be a list. We use structural induction (Definition 10) on \(L\), with \(P(L)\) the equation \(\operatorname{len}\mathopen{}\left(\operatorname{app}(L, M)\right)\mathclose{} = \operatorname{len}(L) + \operatorname{len}(M)\).
Base case. The base case of Definition 6 names only the empty list, and
\[ \begin{aligned} \operatorname{len}\mathopen{}\left(\operatorname{app}(\mathtt{empty}, M)\right)\mathclose{} &= \operatorname{len}(M) && \text{(definition of } \operatorname{app} \text{)} \\&= 0 + \operatorname{len}(M) \\&= \operatorname{len}(\mathtt{empty}) + \operatorname{len}(M). && \text{(definition of } \operatorname{len} \text{)} \end{aligned} \]
Inductive step. The recursive rule builds \(\operatorname{cons}(x, L)\) from an element \(x\) and a list \(L\). Suppose \(P(L)\). Then
\[ \begin{aligned} & \operatorname{len}\mathopen{}\left(\operatorname{app}\mathopen{}\left(\operatorname{cons}(x, L), M\right)\mathclose{}\right)\mathclose{} \\&= \operatorname{len}\mathopen{}\left(\operatorname{cons}\mathopen{}\left(x, \operatorname{app}(L, M)\right)\mathclose{}\right)\mathclose{} && \text{(definition of } \operatorname{app} \text{)} \\&= 1 + \operatorname{len}\mathopen{}\left(\operatorname{app}(L, M)\right)\mathclose{} && \text{(definition of } \operatorname{len} \text{)} \\&= 1 + \operatorname{len}(L) + \operatorname{len}(M) && \text{(induction hypothesis } P(L) \text{)} \\&= \operatorname{len}\mathopen{}\left(\operatorname{cons}(x, L)\right)\mathclose{} + \operatorname{len}(M), && \text{(definition of } \operatorname{len} \text{)} \end{aligned} \]
which is \(P\mathopen{}\left(\operatorname{cons}(x, L)\right)\mathclose{}\). By structural induction, \(P(L)\) holds for every list \(L\). Since \(M\) was any list, the equation holds for all lists \(L\) and \(M\).
Proof. A natural number \(n\) has \(m\) digits exactly when \(10^{m - 1} \le n < 10^m\), since \(10^{m - 1}\), which is 1 followed by \(m - 1\) zeros, is the smallest natural number with \(m\) digits, and \(10^m\), which is 1 followed by \(m\) zeros, is the smallest with \(m + 1\). So the inequalities say that \(n\) has \(d(n)\) digits.
The recursive rule uses \(d\mathopen{}\left(\mathopen{}\left\lfloor n / 10\right\rfloor\mathclose{}\right)\mathclose{}\), not \(d(n - 1)\), so we use strong induction (Definition: Proof by strong induction). Let \(n\) be a natural number, and suppose the inequalities hold for every natural number less than \(n\).
If \(n \le 9\), then \(d(n) = 1\), and \(10^0 = 1 \le n < 10 = 10^1\).
If \(n \ge 10\), let \(q = \mathopen{}\left\lfloor n / 10\right\rfloor\mathclose{}\). Then \(q \le n / 10 < q + 1\), so \(10q \le n < 10q + 10\). Also \(q \ge 1\), since \(n / 10 \ge 1\), and \(q < n\), so \(q\) is a natural number less than \(n\), and by the strong induction hypothesis
\[ 10^{d(q) - 1} \le q < 10^{d(q)}. \]
The recursive rule gives \(d(n) = 1 + d(q)\). For the lower bound,
\[ \begin{aligned} n &\ge 10 q \\&\ge 10 \cdot 10^{d(q) - 1} && \text{(strong induction hypothesis)} \\&= 10^{d(q)} \\&= 10^{d(n) - 1}. && \text{(} d(n) = 1 + d(q) \text{)} \end{aligned} \]
For the upper bound, \(q\) and \(10^{d(q)}\) are integers with \(q < 10^{d(q)}\), so \(q + 1 \le 10^{d(q)}\), and
\[ \begin{aligned} n &< 10 (q + 1) \\&\le 10 \cdot 10^{d(q)} && \text{(} q + 1 \le 10^{d(q)} \text{)} \\&= 10^{d(q) + 1} \\&= 10^{d(n)}. && \text{(} d(n) = 1 + d(q) \text{)} \end{aligned} \]
So the inequalities hold for \(n\), and by strong induction they hold for every natural number \(n\).