base case: The stopping case with no recursive call; call frame: The stored information for one active function call; progress: A change that moves toward the base case
55-minute lesson sequence
0–5 · Answer the retrieval alone. Compare one reason.
5–15 · Teacher models the example. Students predict each next step.
15–40 · Build, trace and test the main tasks. Work alone first; compare after a checkpoint.
40–50 · Check results. Explain a test and repair one error. Use the extension only if ready.
50–55 · Complete the exit alone. Save the evidence for the next lesson.
Try before notes
For sum_to_n(2) with a zero base case, list the call inputs and the final answer.
Build the idea
Recursion solves a problem using a smaller instance of the same problem. Every call has its own current parameters and waits for the recursive result. Draw one call at a time rather than treating all calls as one changing variable.
For a list sum with index starting at zero, the base case is index==len(values). It returns 0 without another call. The recursive step adds values[index] to the sum starting at index+1. This progress eventually reaches the base case under the stated contract.
Returns unwind in reverse call order. An n-item list uses n+1 calls including the base call in this model. Python recursion uses call-stack space and has a recursion limit, so a loop is often preferable for a very long list.
sum_from([4, 6], 0) adds the value at index 0 to the sum from index 1. Calls use indices 0, 1, 2. Index 2 equals the list length, so the base call returns 0. The waiting returns are 6+0=6, then 4+6=10. The list is unchanged. Preconditions: values is a list of numbers; start index is 0; each call advances by 1.
def sum_from(values, index):
if index == len(values):
return 0
return values[index] + sum_from(values, index + 1)
Predict then reveal
Trace sum_from([8,1],0), including the base call and return values.
Reveal reasoning
Index 0 waits for 8+sum_from(...,1). Index 1 waits for 1+sum_from(...,2). Index 2 equals length 2 and returns 0. Then index 1 returns 1; index 0 returns 9. There are three calls.
If the recursive call uses index rather than index+1, what happens for [8]? Why might [] hide the bug?
Reveal reasoning
For [8] at index 0, each call repeats index 0 and never reaches length 1; Python eventually raises RecursionError. For [] at index 0, the base case is already true, so no recursive step is tested and it returns 0.
Work one step together
Read the first task. Each learner predicts the first step privately. Compare the method, then complete one intermediate row or one code iteration together. Do not display a finished workbook answer.
Practice
Trace sum_from([3, 5, 2], 0). Write the index and expression for each call, then the return values from the base case outward. State the number of calls, including the base call.
Your turn
Complete the remaining steps of task 1 alone, then the next tasks. Predict before running code. Save your own evidence before comparing.
Practice
Trace sum_from([3, 5, 2], 0). Write the index and expression for each call, then the return values from the base case outward. State the number of calls, including the base call.
Build the function. Test [], [7] and [3, 5, 2], always starting at index 0. Then build a loop version and compare results. Explain one reason a loop may be better for a very long list in Python.
A student changes index+1 to index. Explain what happens for [7]. Is the empty-list test alone enough to find the error?
Check and explain
Writing a base case is not enough; each recursive step must make progress toward it.
Point to the first step, row, condition or requirement that supports your answer. If results differ, find the first different state and repair one cause.
Show what you know
State the base case and the progress step in the correct function.
HL challenge when ready
Why is there one recursive call per non-base call even though several calls are waiting?
Week 9 homework
Show working. Keep code and output. Stop at 25 minutes and mark where you reached.
Complete this homework before Friday 15 January. Trace sum_from([2, 5], 0), using the lesson 9.3 function. Give call indices, base return, final result and total calls.
For a queue initially [A, B, C], serve one item from the front and add D to the back. State the served item and the queue after both actions. Explain FIFO in one sentence.
Prepare your four evidence items for lesson 9.5. Name one item you will explain, the input or question, and the correct output or conclusion. Do not create a long new report.
Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.
A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.
Prompt 1
Trace recursive length on a short list, including the empty-list case.
No imports needed.
Hint and starter code
values[1:] makes a new list without the first item; copying uses extra memory. Keep inputs small.
# values[1:] makes a new list without the first item; copying uses extra memory.
def length(values):
if not values:
return 0
return 1 + length(values[1:])
print(length([4, 7, 9]))
Compare recursive and loop totals for the same small list. Complete the loop total.
No imports needed.
Hint and starter code
pass does nothing but keeps a block syntactically valid while you complete it.
# pass does nothing but keeps a block syntactically valid while you complete it.
values = [2, 4, 6]
loop_total = 0
for value in values:
pass # Your turn: add value to loop_total
print(loop_total)