Documente Academic
Documente Profesional
Documente Cultură
5
Linear recursion and iteration, 1.2.1
Martin Henz
Before we start...
Where are we in CS1101S?
What is CS1101S? Source Academy, Studios, Missions, Piazza,
Paths
This week
Path P2A: out today, to complete by tomorrow
Reflection R2: sheet out today, come to reflection session
tomorrow
Mission M2A: out tomorrow, due next Monday
Quest Q2A: out tomorrow, due next Tuesday
Mission M2B: out on Friday, due next Wednesday
Quest Q2B: out on Friday, due next Thursday
More on Quests in B2
CS1101S: Programming Methodology L2: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Linear recursion and iteration, 1.2.1
1 + 2 * 3 + 4
(1 + (2 * 3)) + 4
(1 + 6) + 4
7 + 4
13
Problem
You need to find the cost of a circular handrail, knowing that the
cost of the rail per meter is 199.50 dollars, and that the radius of
the circle is 2.1 meters.
Solution
CS1101S: Programming Methodology L2: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Linear recursion and iteration, 1.2.1
Applicative vs normal order reduction, 1.1.5
f(5)
-> sum_of_squares (5 + 1, 5 * 2)
f(5)
-> sum_of_squares (5 + 1, 5 * 2)
-> sq (5 + 1) + sq (5 * 2)
Some abbreviations
turn_upsidedown → tud
quarter_turn_right → qtr
quarter_turn_left → qtl
function tud(rune) {
return qtr(qtr(rune ));
}
function qtl(rune) {
return qtr(tud(rune ));
}
show(qtl(heart ))
Abbreviation
For brevity, we write sf instead of stack_frac.
Examples
Observation
The solution for n is computed using solution n − 1,
the solution for n − 1 is computed using solution n − 2, etc
until we reach a case that we can solve trivially.
A Recipe
Another example
Recursive process
The applications of pat accumulate as a result of the recursive
calls. They are deferred operations.
Iterative process
With applicative order reduction, the pat function is applied before
the recursive call. There is no deferred operation.
Factorial
n! = n(n − 1)(n − 2) · · · 1
Grouping
n! = n((n − 1)(n − 2) · · · 1)
n! = 1 if n = 1
= n(n − 1)! if n > 1
n! = 1 if n = 1
= n(n − 1)! if n > 1
Factorial in Source
factorial (4)
4 * factorial (3)
4 * (3 * factorial (2))
4 * (3 * (2 * factorial (1)))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * 6
24
Notice the build-up of pending (“deferred”) operations.
This factorial function gives rise to a recursive process.
CS1101S: Programming Methodology L2: Substitution Model, Recursion
stackn and repeat_pattern
Substitution model, 1.1.3 and 1.1.5
Simple factorial and its recursive process, 1.2.1
Linear recursion and iteration, 1.2.1
Fibonacci function, 1.2.2
Dimensions of performance
Time: how long does the program run
Space: how much memory do we need to run the program
Number of operations
grows linearly proportional to n.
factorial (4)
4 * factorial (3)
4 * (3 * factorial (2))
4 * (3 * (2 * factorial (1)))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * 6
24
factorial (4)
4 * factorial (3)
4 * (3 * factorial (2))
4 * (3 * (2 * factorial (1)))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * 6
24
function fib(n) {
return n <= 1 ? n
: fib(n - 1) + fib(n - 2);
}
function f(n, k, x, y) {
return (k > n)
? y
: f(n, k + 1, y, x + y);
}
function fib(n) {
return (n < 2) ? n : f(n, 2, 0, 1);
}
Summary