Class 16: More Recursion with Lists

Reading: Repetition with Recursion. Lab: Repetition with Recursion. Back to Recursion with Lists. On to Recursion with Natural Numbers.

Held Friday, February 16, 2001

Summary

Today we continue our exploration of recursion over lists through experimentation and reflection.

Due

Assignments

Notes

• On the days when homeworks are due, I get a lot of email whose title is simply "Homework" or "Homework 2" or whatever. I file those away until I'm ready to grade. If you need help on a homework, please title your message Help on Homework or something similar.
• Many of you clearly did not finish HW2 (or at least didn't get it working).
• You need to start earlier.
• You may have an extension until Monday.
• However, you do still have other work to do for the course.
• I'll do my best to get HW2 graded over the weekend. I again plan to post my comments publically.
• You have two assignments for next Friday, a lab writeup and a homework.
• Some people were confused about the take home: I'm distributing it next Friday.

Overview

Recursively Defined Data

• You may not have gotten this idea from the readings, but we define more than procedures recursively: We also define some data recursively.
• Lists: A list is either
• the empty list (base case)
• the cons of a value and a list (recursive case)
• Natural numbers: A natural number is either
• the value 0 (base case)
• one plus a natural number
• ...

Squaring Values

• Let's talk together about the problem of squaring all the values in a list.
• My one-on-one discussions have suggested some ways to look at the problem that may help others of you who had difficulties.
• Here's the problem: We need to square every element in a list and create a new list of those squares.
• Remember that the basic point of recursion is to
• break up the problem into a small subproblem and a big subproblem (but still smaller than the original problem),
• solve the small problem,
• assume you can solve the big subproblem
• Let's begin with the recursive case.
• Suppose you know how to create a list containing the square of every element but the first.
• How can you use that result to create a list containing the square of every element?
• Now let's consider the base case. For recursion over lists, the empty list, the typical base case involves an empty list or the singleton list.
• What is the result of squaring every value in the empty list?
• It may help to think about the kind of valur our procedure is supposed to return (is it a number, a symbol, a string, a list, or something else).

Reflection

Patterns

• Let's try to identify some patterns of recursion over lists.
• That is, the general structure of kinds of Scheme procedures that work on lists.
• Here's one category of recursion that we did a few times: apply some operation to every element of a list.
• How do we express the general form of that category?
• What other kinds of things do you do to lists?
• What is the general form for those things?

Problems

• What things are likely to be difficult or wrong in one of your recursive procedures?
• I see many people having trouble with the base case. It may help to think about what kind of thing the procedure should return.
• I know that you sometimes write procedures that run forever (I do, too; I wrote one yesterday). Make sure that when you do the recursive call, you're using something closer to the base case.

History

Friday, 12 January 2001

• Created generic outline format for class.

Friday, 16 February 2001

• Filled in the details.

Back to Recursion with Lists. On to Recursion with Natural Numbers.

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

This page was generated by Siteweaver on Wed May 5 12:14:54 2004.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/outline.16.html`.