Held: Monday, 22 September 2003
Today we begin to consider one of the most powerful tools in Scheme,
recursion. Recursion allows you to repeat operations.
- Sorry about not getting homework 2 online sooner. As I mentioned in class, the intent was simply that you finish the pirate lab.
- Don't worry if you don't finish today's lab today. There's more time tomorrow (and no extra reading).
- I've graded lab writeup 1 and mailed you my comments. We'll spend a few minutes discussing my notes on the assignment.
- You may recall that when we first considered algorithms we identified
a number of key aspects of algortihms:
- variables: the ability to name things;
- conditions: the ability to choose between things;
- procedures: the ability to name (and parameterize)
collections of steps;
- repetition: the ability to do something multiple times;
- input and output: the ability to get and report
- We've already seen how to do almost all of these things,
except for repetition.
- Examples of repetition from baking:
- Stir the mix 50 times
- Knead the bread dough until it feels like your earlobe
- Bake until golden-brown.
- Examples of repetition from mathematics:
- Sum these values
- Find the smallest of these values
- Examples of repetition from everyday life:
- Naively find a name in the phone book
- Do I have a CD by Van Morrison?
- Examples of repetition from previous Scheme exercises:
- How long is this list?
- Is X a member of this list?
- In Scheme, the most common mechanism for repetition is
- To do something that involves repeated actions, you
- Do one action
- Repeat the rest
- Combine the results if necessary.
- For example, to stir your cake mix 50 times, you stir it one time
and then stir it 49 more times.
- More generally, to stir a cake mix n times, you stir it one
time and then n-1 more times.
- Similarly, to knead dough until its the right consistency, you
knead it a little, check the consistency, and, if it's not the
right consistency, knead it until its the right consistency.
- In the case of mathematics, to sum a list we might add the
first value to the sum of the remaining values (or add the
last value to the sum of the initial values).
- There are a few key aspects to recursive design:
- You need to know when you're done (and what to do when you're done).
This aspect of recursive design is called the base case.
- You need to know what to do when you're not done. Here, you should
do a little, try again, and then perhaps combine the results.
This aspect of recursive design is called the recursive case.
- You need to be sure that you're getting closer to the base case
(otherwise you'll never stop).
- We'll look together at how we might solve a few recursive list-based
examples as a way of thinking about the problem. I've included a
variety of possibilities.
- sum, sum the elements in a list.
Determine if a value is in a list.
Find the length of a list
- We'll assume that the built-in
does not exist.
Find the smallest value in a list of numbers.
Find the average value in a list of numbers.
Add two to each number in a list of numbers.
- increment elements (revised):
Add two to each number in a list of many different kinds of values.
- The code is not in the outline because I don't want to bias your
- Here's the form of a typical recursive procedure:
(combine (partof val)
(proc (update val))))))
- When the value you're working with is a list and your base case
is the null list, the form is
(if (null? lst)
(combine (onestep (car val))
(proc (cdr val))))))
Thursday, 28 August 2003 [Samuel A. Rebelsky]