# Class 14: Repetition with Recursion

Back to Random Fun. On to Recursion with Lists.

Held Monday, September 23, 2002

Summary

Today we begin to consider one of the most powerful tools in Scheme, recursion. Recursion allows you to repeat operations.

Notes:

• I'm running low on toaster pastries. I probably won't be able to replace them until Wednesday, so don't plan on food tomorrow.
• In any case, tomorrow's class is later in the morning, so you should have less need (or so I hope).
• Today's class will replicate the strategy we used on Friday: Lots of group discussion with some Now play with it on the computer time.
• There will probably be a formal lab on this material tomorrow.
• No extra reading, but you should have read Repetition with Recursion.
• I've been asked to remind you that the Preservation Hall Jazz Band plays at 8pm tomorrow night in Herrick.

Overview

• Repetition
• Recursion
• Examples
• Recursion in Scheme

## Repetition

• 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 values.
• 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
• 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?

## Recursion

• In Scheme, the most common mechanism for repetition is recursion.
• 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).

## Some Examples

• 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.
• member: Determine if a value is in a list.
• length: Find the length of a list
• We'll assume that the built-in `length` procedure does not exist.
• smallest: Find the smallest value in a list of numbers.
• average: Find the average value in a list of numbers.
• increment-elements: 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 answers.

## Recursion in Scheme

• Here's the form of a typical recursive procedure:
```(define proc
(lambda (val)
(if (base-case-test)
(base-case val)
(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 somewhat simpler:
```(define proc
(lambda (lst)
(if (null? lst)
null-case
(combine (onestep (car val))
(proc (cdr val))))))
```

## History

Thursday, 29 August 2002 [Samuel A. Rebelsky]

Monday, 23 September 2002 [Samuel A. Rebelsky]

• Added section on example problems.

Back to Random Fun. On to Recursion with Lists.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Dec 2 08:41:04 2002.
The source to the document was last modified on Mon Sep 23 08:24:59 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.14.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu