Recursively Defined Functions
Recursion Rosen 5th ed., §§3.4
§§3.4
Recursive Definitions
• Simplest case: One way to define a
function f:N→S (for any set S) or series
an=f(n) is to:
• Recursion
– define object in terms of itself
– can be used to define sequences,
functions, and sets
– Define f(0).
– For n>0, define f(n) in terms of f(0),…,f(n−1).
• Example
sequence {an},
an = 2n
• E.g.: Define the series an :≡ 2n recursively:
recursion
– Let a0 :≡ 1.
– For n>0, let an :≡ 2an-1.
a0 = 1; an+1 = 2an
1
Another Example
Recursive definition of Factorial
• Give an inductive definition of the factorial
function F(n) :≡ n! :≡ 2⋅3⋅…⋅n.
• Suppose we define f(n) for all n∈N
recursively by:
– Base case: F(0) :≡ 1
– Recursive part: F(n) :≡ n ⋅ F(n-1).
– Let f(0)=3
– For all n∈N, let f(n+1)=2f(n)+3
• F(1)=1
• F(2)=2
• F(3)=6
• What are the values of the following?
– f(1)= 9 f(2)= 21 f(3)= 45 f(4)=93
3
The Fibonacci Series
4
Inductive Proof about Fib. series
• The Fibonacci series fn≥0 is a famous
series defined by:
f0 :≡ 0, f1 :≡ 1, fn≥2 :≡ fn−1 + fn−2
0
1 1
2 3
58
13
2
Leonardo Fibonacci
1170-1250
5
• Theorem: fn < 2n.
Implicitly for all n∈N
• Proof: By induction.
Note use of
Base cases: f0 = 0 < 20 = 1
base cases of
1
f1 = 1 < 2 = 2
recursive def’n.
Inductive step: Use 2nd principle of
induction
(strong induction). Assume ∀j<k, fj < 2j.
Then fk = fk−1 + fk−2 is
< 2k−1 + 2k−2 < 2k−1 + 2k−1 = 2k. ■
6
1
Recursively Defined Sets
The Set of All Strings
• An infinite set S may be defined
recursively, by giving:
• Σ: an alphabet (字母表)
e.g. Σ={a, b, c}, Σ={α, β, χ, δ}
– A small finite set of base elements of S.
– A rule for constructing new elements of S from
previously-established elements.
– Implicitly, S has no other elements but these.
• Example: Let 3∈S, and let x+y∈S if x,y∈S.
What is S?
• Σ*: the set of all strings over Σ
e.g. if Σ={a, b, c}, Σ* ={λ, a, b, c, aa, ab, ac,
ba, bb, bc, ca, cb, cc, aaa, aab, aac, …}
• Σ* can be recursively defined as:
base t
λ ∈ Σ* (λ :≡ “”, the empty string) elemen
w ∈ Σ* ∧ x ∈ Σ → wx ∈ Σ* constructing
rule
7
Σ* Example
Use Recursion to Define Operators
• Given Σ={a, 1, %}, what is Σ* ?
λ ∈ Σ*
base nt
e
elem
w ∈ Σ* ∧ x ∈ Σ → wx ∈ Σ*
cons
rule
• Concatenation (•): combine two strings
abc • def = abcdef
abc • λ = abc
ng
tructi
• Define recursively
So a ∈ Σ*, 1 ∈ Σ*, and % ∈ Σ*
the
apply gain
rule a
the
apply gain
rule a
8
– Base step: If w ∈ Σ* then w • λ = w
– Recursive step: If w1∈Σ* and w2∈Σ* and
x∈Σ then w1 • (w2 x) = (w1 • w2)x
aa ∈ Σ*, a1 ∈ Σ*, a% ∈ Σ*, 1a ∈ Σ*, …
aaa ∈ Σ*, aa1 ∈ Σ*, aa% ∈ Σ*, a1a ∈ Σ*, …
gain
and a
9
Recursively Defined Operator
Example
iv
recurs
step
String Length: Recursive Definition
• l(w): the length of the string w
• Basis step
e
(abc • de)f
abc • def
w1=abc, w2=de, x=f
l(λ) = 0
(abc • d)e
abc • de
• Recursive step
w1=abc, w2=d, x=e
l(wx) = l(w) + 1
(abc • λ)d
abc • d
w1=abc, w2= λ, x=d
s
base
abc • λ
10
l(abc) = l(ab) + 1
if w∈Σ* and x∈Σ
l(a) = l(λ) + 1
tep
l(ab) = l(a) + 1
abc
11
l(λ) = 0
12
2
© Copyright 2026 Paperzz