# Class 15: Recursion with Lists

Back to Repetition with Recursion. On to More Recursion with Lists.

Held Tuesday, September 24, 2002

Summary

Today we investigate recursion over lists with a number of experiments.

Assignments

Notes:

• Are there any final questions on homework 3?
• Any food choices other than pop-tarts for tomorrow?
• I'm still trying to find time to grade homework 2.
• Yes, I plan to have today's lab take two days.

Overview

## Reflection

• I find it useful to think about the pattern one uses to design certain kinds of procedures.
• Here's the pattern 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))))))
```
• You may even find that there are some boolean procedures that don't even need an `if`. For example, here's one implementation of member that works on the principle that `val` is a member of `lst` if the list is not empty and `val` is the first element of `lst` or `val` is a member of the remainder of `lst`.
```(define member?
(lambda (val lst)
(and (not (null? lst))
(or (equal? val (car lst))
(member? val (cdr lst))))))
```

## Lab

• Start working on the lab.
• Depending on how things go today, we may continue it tomorrow.

## History

Thursday, 29 August 2002 [Samuel A. Rebelsky]

Tuesday, 24 September 2002 [Samuel A. Rebelsky]

• Filled in the reflection section.

Back to Repetition with Recursion. On to More 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:06 2002.
The source to the document was last modified on Tue Sep 24 09:07:19 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.15.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu