# Class 29: Deep Recursion

Back to Pairs and Pair Structures. On to Early Break.

Held: Thursday, 16 October 2003

Summary: Today we consider a variant of recursion known as deep recursion. In this variant, you often recurse in multiple directions, particularly into the `car` of each pair as well as into the `cdr` of each pair.

Related Pages:

Assignments

• Have a great break. Behave yourselves.

Notes:

• Reminder: We do not have class on Friday.
• Have a pleasant break.

Overview:

• Lists, revisited.
• List recursion, revisited.
• Trees, introduced.
• Deep recursion, considered.
• Group design example 1: `count-elements`.
• Group design example 2: `depth`.
• Lab.

## Review: What is a List?

• As you may have noticed from our work to date, lists are an essential part of Scheme programming.
• However, we have thought about lists in a number of different ways.
• When we see a list, we think of it as a group of data in order.
• When we build or use a list, we think of it in terms of the key operations, `cons`, `car`, `cdr`, `null`, and `null?`.
• In writing `listp?`, we thought about the list as a recursively-defined data structure. A list is either
• The empty list or
• Cons of a value and a list.

## Review: List Recursion

• Note that this recursive definition of lists gives you a natural way of recursing over lists.
• With the most typical base case (that is, the base case occurs when the list is null), we write something like
```(define procname
(lambda (lst)
(if (null? lst)
base-case
(combine (car lst) (procname (cdr lst))))))
```
• `do-something` usually does something fairly simple with the combination of the car and the recursive call.

## Complicating Structures: Nested Lists and Trees

• There are, of course, other interesting ways to combine values.
• As you've seen, lists can certainly contain other lists. We often refer to such lists as nested lists.
• How do we define nested lists? We might limit our definition above slightly
• A nested list is a list in which one of the component values is a list.
• Note that it will be harder to come up with a nice recursive definition, since some component values will be lists, and some will not.
• Note that there are other interesting things we can do with cons cells. In particular, if we don't limit the last value to null, we can define what is traditionally called a tree
• Any simple value (string, symbol, procedure, number, character, or boolean) is a tree.
• Cons of any two trees is a tree.
• Computer scientists have found a wide variety of applications for trees and tree-like structures.

## Complicating Procedures: Deep Recursion

• Note that we may recurse differently over trees and nested lists.
• In particular, if the car of a list or pair is a list (or any pair structure), you might want to recurse over that structure, too.
• And if the car list has its own element list, we'd like to recurse on that list, too.
• Does this interesting theoretical approach have any practical implications?
• That is, "Are there ways we'd want to use this?"
• There are certainly many cases in which it helps to have a helper of this form.
• Note that the form of recursion, in which you recurse over sublists as well as along the top-level list, is typcially called deep recursion.
• There are many common problems and algorithms for which deep recursion is an appropriate technique.

## Extending `length`

• Some of you have noted that a key deficiency of `length` is that it does not count the values in sublists when determining the length of the list. Hence, the length of the list `((1 2 3))` is 1, rather than the 3 that most people might expect.
• Let's try to design a procedure that counts all the elements in a list or its sublists.
• Design:
• What is the base case?
• What value do we return in the base case?
• What are some recursive cases?
• What do we do in the recursive cases?
• Implementation:
• How do we translate all that to Scheme?
• Testing:
• Choose some interesting tests.
• Here's one `(list null null null)`

## Example: Determining the Depth of a Nested List

• When one builds a tree or list, it may be useful to ask how far one might have to go in the tree before one reaches a value.
• We call the furthest such length in a tree the depth of a tree.
• How could we write such a procedure?
• Design:
• What is the base case?
• What value do we return in the base case?
• What are some recursive cases?
• What do we do in the recursive cases?
• Implementation:
• How do we translate all that to Scheme?
• Testing:
• Identify some interesting tests.

## History

Thursday, 28 August 2003 [Samuel A. Rebelsky]

• Created automatically.

Back to Pairs and Pair Structures. On to Early Break.

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 Tue Dec 9 13:59:47 2003.
The source to the document was last modified on Mon Sep 1 13:30:51 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Outlines/outline.29.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu