Substitution model, 1.1.3 and 1.1.
5
Recursion and iteration, 1.2.1 and 1.2.2
L2A: Substitution Model, Recursion
CS1101S: Programming Methodology
Boyd Anderson
August 21, 2024
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
Path P2A: out today, to complete by tomorrow
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
Path P2A: out today, to complete by tomorrow
Reflection R2: sheet out on Canvas, please prepare before
your reflection session tomorrow
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
Path P2A: out today, to complete by tomorrow
Reflection R2: sheet out on Canvas, please prepare before
your reflection session tomorrow
Mission M2A: out today after lecture
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
Path P2A: out today, to complete by tomorrow
Reflection R2: sheet out on Canvas, please prepare before
your reflection session tomorrow
Mission M2A: out today after lecture
Quest Q2A: out today evening
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
Path P2A: out today, to complete by tomorrow
Reflection R2: sheet out on Canvas, please prepare before
your reflection session tomorrow
Mission M2A: out today after lecture
Quest Q2A: out today evening
Mission M2B: out tomorrow
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
Path P2A: out today, to complete by tomorrow
Reflection R2: sheet out on Canvas, please prepare before
your reflection session tomorrow
Mission M2A: out today after lecture
Quest Q2A: out today evening
Mission M2B: out tomorrow
Quest Q2B: out on Friday
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Before we start...
Where are we in CS1101S?
What is CS1101S? SICP JS, Source Academy, Studios, Missions,
Ed, Paths
Getting into gear
Path P2A: out today, to complete by tomorrow
Reflection R2: sheet out on Canvas, please prepare before
your reflection session tomorrow
Mission M2A: out today after lecture
Quest Q2A: out today evening
Mission M2B: out tomorrow
Quest Q2B: out on Friday
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
Quests (optional homework): supplement your XP
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
Quests (optional homework): supplement your XP
Early Mission/Quest submission bonus: 75 XP within 3 days;
bonus decays after that
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
Quests (optional homework): supplement your XP
Early Mission/Quest submission bonus: 75 XP within 3 days;
bonus decays after that
Avengers can “unsubmit” your submission, within reasonable
limits
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
Quests (optional homework): supplement your XP
Early Mission/Quest submission bonus: 75 XP within 3 days;
bonus decays after that
Avengers can “unsubmit” your submission, within reasonable
limits
If you submit less than 2 days after release of Mission/Quest,
Avengers will give feedback less than 4 days after release.
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
Quests (optional homework): supplement your XP
Early Mission/Quest submission bonus: 75 XP within 3 days;
bonus decays after that
Avengers can “unsubmit” your submission, within reasonable
limits
If you submit less than 2 days after release of Mission/Quest,
Avengers will give feedback less than 4 days after release.
Path: XP for submission
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
Quests (optional homework): supplement your XP
Early Mission/Quest submission bonus: 75 XP within 3 days;
bonus decays after that
Avengers can “unsubmit” your submission, within reasonable
limits
If you submit less than 2 days after release of Mission/Quest,
Avengers will give feedback less than 4 days after release.
Path: XP for submission
Early path submission: 25 XP within 1 day; bonus decays
after that
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Earning eXperience Points
Missions (basic homework): your primary source of XP
Quests (optional homework): supplement your XP
Early Mission/Quest submission bonus: 75 XP within 3 days;
bonus decays after that
Avengers can “unsubmit” your submission, within reasonable
limits
If you submit less than 2 days after release of Mission/Quest,
Avengers will give feedback less than 4 days after release.
Path: XP for submission
Early path submission: 25 XP within 1 day; bonus decays
after that
No “unsubmit” for paths
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Need help?
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Need help?
ED forums:
https://edstem.org/us/courses/62100/discussion/
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Need help?
ED forums:
https://edstem.org/us/courses/62100/discussion/
PM your Avenger!
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Need help?
ED forums:
https://edstem.org/us/courses/62100/discussion/
PM your Avenger!
Forum Hours. Every Tuesday 6:30pm-8:30pm, starting from
Week 3. Venue is COM1 basement.
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Substitution model, 1.1.3 and 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
1 Substitution model, 1.1.3 and 1.1.5
2 Recursion and iteration, 1.2.1 and 1.2.2
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
1 Substitution model, 1.1.3 and 1.1.5
Evaluating combinations, 1.1.3
Evaluating function application, 1.1.5
Applicative vs normal order reduction, 1.1.5
2 Recursion and iteration, 1.2.1 and 1.2.2
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Review: Evaluating arithmetic expressions, see 1.1.3
1 + 2 * 3 + 4
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Review: Evaluating arithmetic expressions, see 1.1.3
1 + 2 * 3 + 4
Replace “eligible” subexpression with result
(1 + (2 * 3)) + 4
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Review: Evaluating arithmetic expressions, see 1.1.3
1 + 2 * 3 + 4
Replace “eligible” subexpression with result
(1 + (2 * 3)) + 4
(1 + 6) + 4
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Review: Evaluating arithmetic expressions, see 1.1.3
1 + 2 * 3 + 4
Replace “eligible” subexpression with result
(1 + (2 * 3)) + 4
(1 + 6) + 4
7 + 4
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Review: Evaluating arithmetic expressions, see 1.1.3
1 + 2 * 3 + 4
Replace “eligible” subexpression with result
(1 + (2 * 3)) + 4
(1 + 6) + 4
7 + 4
11
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Example for substitution model 1.1.5
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Example for substitution model 1.1.5
Problem
You need to find the cost of a circular
handrail, knowing that the cost of the
rail per meter is 199.95 dollars, and
the radius of the circle is 2.1 meters.
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Example for substitution model 1.1.5
Problem
You need to find the cost of a circular
handrail, knowing that the cost of the
rail per meter is 199.95 dollars, and
the radius of the circle is 2.1 meters.
Solution
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Evaluation of function application (1.1.5)
const cost_per_meter = 199.95;
function circumference ( radius ) {
return 2 * math_PI * radius ;}
function c o s t_ o f _ c i r c u l a r _ h a n d r a i l ( r ) {
return cost_per_meter * circumference ( r );}
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Evaluation of function application (1.1.5)
const cost_per_meter = 199.95;
function circumference ( radius ) {
return 2 * math_PI * radius ;}
function c o s t_ o f _ c i r c u l a r _ h a n d r a i l ( r ) {
return cost_per_meter * circumference ( r );}
c o s t _ o f _ c i r c u la r _ h a n d r a i l (2.1)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Evaluation of function application (1.1.5)
const cost_per_meter = 199.95;
function circumference ( radius ) {
return 2 * math_PI * radius ;}
function c o s t_ o f _ c i r c u l a r _ h a n d r a i l ( r ) {
return cost_per_meter * circumference ( r );}
c o s t _ o f _ c i r c u la r _ h a n d r a i l (2.1)
-> 199.5 * circumference (2.1)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Evaluation of function application (1.1.5)
const cost_per_meter = 199.95;
function circumference ( radius ) {
return 2 * math_PI * radius ;}
function c o s t_ o f _ c i r c u l a r _ h a n d r a i l ( r ) {
return cost_per_meter * circumference ( r );}
c o s t _ o f _ c i r c u la r _ h a n d r a i l (2.1)
-> 199.5 * circumference (2.1)
-> 199.5 * ((2 * 3.141592) * 2.1)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Evaluation of function application (1.1.5)
const cost_per_meter = 199.95;
function circumference ( radius ) {
return 2 * math_PI * radius ;}
function c o s t_ o f _ c i r c u l a r _ h a n d r a i l ( r ) {
return cost_per_meter * circumference ( r );}
c o s t _ o f _ c i r c u la r _ h a n d r a i l (2.1)
-> 199.5 * circumference (2.1)
-> 199.5 * ((2 * 3.141592) * 2.1)
-> 199.5 * (6.283185 * 2.1)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Evaluation of function application (1.1.5)
const cost_per_meter = 199.95;
function circumference ( radius ) {
return 2 * math_PI * radius ;}
function c o s t_ o f _ c i r c u l a r _ h a n d r a i l ( r ) {
return cost_per_meter * circumference ( r );}
c o s t _ o f _ c i r c u la r _ h a n d r a i l (2.1)
-> 199.5 * circumference (2.1)
-> 199.5 * ((2 * 3.141592) * 2.1)
-> 199.5 * (6.283185 * 2.1)
-> 199.5 * 13.19468
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Evaluation of function application (1.1.5)
const cost_per_meter = 199.95;
function circumference ( radius ) {
return 2 * math_PI * radius ;}
function c o s t_ o f _ c i r c u l a r _ h a n d r a i l ( r ) {
return cost_per_meter * circumference ( r );}
c o s t _ o f _ c i r c u la r _ h a n d r a i l (2.1)
-> 199.5 * circumference (2.1)
-> 199.5 * ((2 * 3.141592) * 2.1)
-> 199.5 * (6.283185 * 2.1)
-> 199.5 * 13.19468
-> 2632.340
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sum_of_sqs (6 , 10)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sum_of_sqs (6 , 10)
-> sq (6) + sq (10)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sum_of_sqs (6 , 10)
-> sq (6) + sq (10)
-> (6 * 6) + (10 * 10)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sum_of_sqs (6 , 10)
-> sq (6) + sq (10)
-> (6 * 6) + (10 * 10)
..
.
-> 136
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Applicative order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sum_of_sqs (6 , 10)
-> sq (6) + sq (10)
-> (6 * 6) + (10 * 10)
..
.
-> 136
Program Link
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Normal order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Normal order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Normal order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Normal order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sq (5 + 1) + sq (5 * 2)
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Normal order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sq (5 + 1) + sq (5 * 2)
-> ((5 + 1) * (5 + 1)) +
((5 * 2) * (5 * 2))
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Normal order reduction, 1.1.5
function sq ( x ) { return x * x ; }
function sum_of_sqs (x , y ) { return sq ( x )+ sq ( y );}
function f ( a ) { return sum_of_sqs ( a +1 , a *2);}
f (5)
-> sum_of_sqs (5 + 1 , 5 * 2)
-> sq (5 + 1) + sq (5 * 2)
-> ((5 + 1) * (5 + 1)) +
((5 * 2) * (5 * 2))
..
.
-> 136
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for applicative-order languages
Primitive expressions : take the value
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for applicative-order languages
Primitive expressions : take the value
Operator combinations : evaluate operands, apply operator
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for applicative-order languages
Primitive expressions : take the value
Operator combinations : evaluate operands, apply operator
Constant declaration : evaluate the value expression and replace
the name by value
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for applicative-order languages
Primitive expressions : take the value
Operator combinations : evaluate operands, apply operator
Constant declaration : evaluate the value expression and replace
the name by value
Function application : evaluate component expressions
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for applicative-order languages
Primitive expressions : take the value
Operator combinations : evaluate operands, apply operator
Constant declaration : evaluate the value expression and replace
the name by value
Function application : evaluate component expressions
if function is primitive: apply the primitive
function
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for applicative-order languages
Primitive expressions : take the value
Operator combinations : evaluate operands, apply operator
Constant declaration : evaluate the value expression and replace
the name by value
Function application : evaluate component expressions
if function is primitive: apply the primitive
function
if function is compound: substitute argument
values for parameters in body of declaration
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
function turn_upside_down ( rune ) {
return quarter _t ur n_ ri gh t (
quar te r_ tu rn _r ig ht ( rune ));
}
function quarter_tur n_left ( rune ) {
return quarter _t ur n_ ri gh t (
turn_upside_down ( rune ));
}
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
function turn_upside_down ( rune ) {
return quarter _t ur n_ ri gh t (
quar te r_ tu rn _r ig ht ( rune ));
}
function quarter_tur n_left ( rune ) {
return quarter _t ur n_ ri gh t (
turn_upside_down ( rune ));
}
qua rter_turn_le ft ( heart )
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
quarter_turn_l( )
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
quarter_turn_l( )
quarter_turn_r(turn_upside_down( ))
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
quarter_turn_l( )
quarter_turn_r(turn_upside_down( ))
quarter_turn_r(quarter_turn_r(quarter_turn_r( )))
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
quarter_turn_l( )
quarter_turn_r(turn_upside_down( ))
quarter_turn_r(quarter_turn_r(quarter_turn_r( )))
quarter_turn_r(quarter_turn_r( ))
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
quarter_turn_l( )
quarter_turn_r(turn_upside_down( ))
quarter_turn_r(quarter_turn_r(quarter_turn_r( )))
quarter_turn_r(quarter_turn_r( ))
quarter_turn_r( )
CS1101S: Programming Methodology L2A: Substitution Model, Recursion
Evaluating combinations, 1.1.3
Substitution model, 1.1.3 and 1.1.5
Evaluating function application, 1.1.5
Recursion and iteration, 1.2.1 and 1.2.2
Applicative vs normal order reduction, 1.1.5
Substitution model for runes
quarter_turn_l( )
quarter_turn_r(turn_upside_down( ))
quarter_turn_r(quarter_turn_r(quarter_turn_r( )))
quarter_turn_r(quarter_turn_r( ))
quarter_turn_r( )
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
1 Substitution model, 1.1.3 and 1.1.5
2 Recursion and iteration, 1.2.1 and 1.2.2
stackn and repeat_pattern
Simple factorial and its recursive process, 1.2.1
Fibonacci function, 1.2.2
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A new predeclared combination: stack_frac
stack_frac(r, heart, sail)
splits available bounded box such that
heart occupies top fraction r of box
and sail occupies remaining 1 - r of box
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Examples
stack_frac(87 / 100, heart, sail)
splits available bounded box such that heart occupies the top 87%
of box and sail occupies remaining 13% of box
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Examples
stack_frac(87 / 100, heart, sail)
splits available bounded box such that heart occupies the top 87%
of box and sail occupies remaining 13% of box
Trisection of the heart
stack_frac (
1 / 3,
heart ,
stack_frac (
1 / 2,
heart ,
heart ))
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Can we define stackn in Source?
Trisection of the heart
stack_frac (1 / 3 , heart ,
stack_frac (1 / 2 , heart , heart ))
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Can we define stackn in Source?
Trisection of the heart
stack_frac (1 / 3 , heart ,
stack_frac (1 / 2 , heart , heart ))
Quadrisection of the heart
stack_frac (1 / 4 , heart ,
stack_frac (1 / 3 , heart ,
stack_frac (1 / 2 , heart ,
heart )))
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Can we define stackn in Source?
Trisection of the heart
stack_frac (1 / 3 , heart ,
stack_frac (1 / 2 , heart , heart ))
Quadrisection of the heart
stack_frac (1 / 4 , heart ,
stack_frac (1 / 3 , heart ,
stack_frac (1 / 2 , heart ,
heart )))
Can we generalise this idea?
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recursive Function, first try
function stackn (n , rune ) {
return stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
stackn (3 , heart )
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recursive Function, first try
function stackn (n , rune ) {
return stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
stackn (3 , heart )
Computers will follow our orders exactly...
We need to precisely describe how a computational process
should be executed.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
The correct version
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
The correct version
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
Observation
Solution for n computed using
solution n − 1,
solution for n − 1 is computed
using solution n − 2, ...
until we reach trivial case.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recipe
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recipe
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
Recipe for recursion
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recipe
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
Recipe for recursion
Figure out trivial base case
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recipe
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
Recipe for recursion
Figure out trivial base case
Assume you know how to solve
problem for n − 1.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recipe
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
Recipe for recursion
Figure out trivial base case
Assume you know how to solve
problem for n − 1.
How can we solve problem for n?
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recipe
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
Recipe for recursion
Figure out trivial base case
Assume you know how to solve
problem for n − 1.
How can we solve problem for n?
We call this Wishful Thinking. Make a wish and it will come true.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Recipe
function stackn (n , rune ) {
return n === 1
? rune
: stack_frac (1 / n ,
rune ,
stackn ( n - 1 , rune ));
}
Recipe for recursion
Figure out trivial base case
I wish that I already have the solution for a
problem n − 1.
Given that, how can I solve problem for n?
We call this Wishful Thinking. Make a wish and it will come true.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Can we define repeat_pattern in Source?
Remember pre-defined repeat_pattern in module rune
repeat_pattern (3 , make_cross , sail )
// should lead to
make_cross ( make_cross ( make_cross ( sail )))
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Can we define repeat_pattern in Source?
Remember pre-defined repeat_pattern in module rune
repeat_pattern (3 , make_cross , sail )
// should lead to
make_cross ( make_cross ( make_cross ( sail )))
Another example
function square ( x ) { return x * x ; }
repeat_pattern (3 , square , 2)
// should lead to
square ( square ( square (2)))
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
repeat_pattern, our first version
function repeat_pattern (n , pat , init ) {
return n === 0
? init
: pat ( repeat_pattern ( n - 1 , pat , init ));
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
repeat_pattern, our first version
function repeat_pattern (n , pat , init ) {
return n === 0
? init
: pat ( repeat_pattern ( n - 1 , pat , init ));
}
repeat_pattern (
3,
square ,
2);
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
repeat_pattern, our first version
function repeat_pattern (n , pat , init ) {
return n === 0
? init
: pat ( repeat_pattern ( n - 1 , pat , init ));
}
repeat_pattern (
3,
square ,
2);
Recursive process
The applications of pat
accumulate as result of recursive calls.
They are deferred operations.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
repeat_pattern, second version
function repeat_pattern (n , pat , rune ) {
return n === 0
? rune
: repeat_pattern ( n - 1 , pat , pat ( rune ));
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
repeat_pattern, second version
function repeat_pattern (n , pat , rune ) {
return n === 0
? rune
: repeat_pattern ( n - 1 , pat , pat ( rune ));
}
repeat_pattern (
3,
square ,
2);
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
repeat_pattern, second version
function repeat_pattern (n , pat , rune ) {
return n === 0
? rune
: repeat_pattern ( n - 1 , pat , pat ( rune ));
}
repeat_pattern (
3,
square ,
2);
Iterative process
With applicative order reduction,
pat function is applied before
the recursive call.
There is no deferred operation.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Another example: Factorial 1.2.1
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Another example: Factorial 1.2.1
Factorial
n ! = n(n − 1)(n − 2) · · · 1
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Another example: Factorial 1.2.1
Factorial
n ! = n(n − 1)(n − 2) · · · 1
Grouping
n ! = n((n − 1)(n − 2) · · · 1)
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Another example: Factorial 1.2.1
Factorial
n ! = n(n − 1)(n − 2) · · · 1
Grouping
n ! = n((n − 1)(n − 2) · · · 1)
Replacement
n ! = n(n − 1) !
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Another example: Factorial 1.2.1
Factorial
n ! = n(n − 1)(n − 2) · · · 1
Grouping
n ! = n((n − 1)(n − 2) · · · 1)
Replacement
n ! = n(n − 1) !
Remember the base case
n!=1 if n = 1
n ! = (n − 1) ! if n > 1
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Translation into Source
Remember the base case
n! = 1 if n = 1
n ! = n(n − 1) ! if n > 1
Factorial in Source
function factorial ( n ) {
return n === 1
? 1
: n *
factorial ( n - 1);
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Example execution using Substitution Model
function factorial ( n ) {
return n === 1 ? 1 : n * factorial ( n - 1);
}
factorial (4)
4 * factorial (3)
4 * (3 * factorial (2))
4 * (3 * (2 * factorial (1)))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * 6
24
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Example execution using Substitution Model
function factorial ( n ) {
return n === 1 ? 1 : n * factorial ( n - 1);
}
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 deferred operations.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Example execution using Substitution Model
function factorial ( n ) {
return n === 1 ? 1 : n * factorial ( n - 1);
}
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 deferred operations.
Recursive process.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Closer look at performance
Dimensions of performance
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Closer look at performance
Dimensions of performance
Time:
how long does the program run
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
A Closer look at performance
Dimensions of performance
Time:
how long does the program run
Space:
how much memory do we need
to run the program
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Time for calculating n!
Total number of executed 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
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Space for calculating n!
Deferred operations: Number of “things to remember”
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
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Can we write an iterative factorial?
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Can we write an iterative factorial?
function factorial ( n ) {
return iter (1 , 1 , n );
}
function iter ( product , counter , n ) {
return counter > n
? product
: iter ( counter * product ,
counter + 1 ,
n );
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), first attempt
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), first attempt
function fib ( n ) {
return n <= 1
? n
: fib ( n - 1) + fib ( n - 2);
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), first attempt
function fib ( n ) {
return n <= 1
? n
: fib ( n - 1) + fib ( n - 2);
}
Time for exploring the tree
...grows with size of tree.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), first attempt
function fib ( n ) {
return n <= 1
? n
: fib ( n - 1) + fib ( n - 2);
}
Time for exploring the tree
...grows with size of tree.
Tree for fib(n) has F (n + 1) leaves.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), first attempt
function fib ( n ) {
return n <= 1
? n
: fib ( n - 1) + fib ( n - 2);
}
Time for exploring the tree
...grows with size of tree.
Tree for fib(n) has F (n + 1) leaves.
√
j ϕn 1k 1+ 5
F (n) = √ + , where ϕ =
5 2 2
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), first attempt
function fib ( n ) {
return n <= 1
? n
: fib ( n - 1) + fib ( n - 2);
}
Time for exploring the tree
...grows with size of tree.
Tree for fib(n) has F (n + 1) leaves.
√
j ϕn 1k 1+ 5
F (n) = √ + , where ϕ =
5 2 2
Can we write an efficient iterative version?
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), iterative solution
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), iterative solution
function fib ( n ) {
return fib_iter (1 , 0 , n );
}
function fib_iter (a , b , count ) {
return count === 0
? b
: fib_iter ( a + b , a , count - 1);
}
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Fibonacci: Computing F (n), iterative solution
function fib ( n ) {
return fib_iter (1 , 0 , n );
}
function fib_iter (a , b , count ) {
return count === 0
? b
: fib_iter ( a + b , a , count - 1);
}
Time for exploring the tree
...grows proportional to n.
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
Examples for recursive solutions: stackn, repeat_pattern,
factorial, fib
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
Examples for recursive solutions: stackn, repeat_pattern,
factorial, fib
Recursive and iterative processes
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
Examples for recursive solutions: stackn, repeat_pattern,
factorial, fib
Recursive and iterative processes
Resources for computational processes:
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
Examples for recursive solutions: stackn, repeat_pattern,
factorial, fib
Recursive and iterative processes
Resources for computational processes:
time and space
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
Examples for recursive solutions: stackn, repeat_pattern,
factorial, fib
Recursive and iterative processes
Resources for computational processes:
time and space
(more on this on Friday)
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
Examples for recursive solutions: stackn, repeat_pattern,
factorial, fib
Recursive and iterative processes
Resources for computational processes:
time and space
(more on this on Friday)
Two versions for Fibonacci
CS1101S: Programming Methodology L2A: 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
Recursion and iteration, 1.2.1 and 1.2.2
Fibonacci function, 1.2.2
Summary
Substitution allows us to trace the evaluation of expressions
Applicative vs normal order reduction
Function applications are replaced by the return expression
Examples for recursive solutions: stackn, repeat_pattern,
factorial, fib
Recursive and iterative processes
Resources for computational processes:
time and space
(more on this on Friday)
Two versions for Fibonacci:
a bad one and a good one!
CS1101S: Programming Methodology L2A: Substitution Model, Recursion