Assigned: Wednesday, 29 January 2003
Due: Tuesday, 4 February 2003
Summary: In this assignment, you will investigate an extension of recursion known as deep recursion.
Purposes: To give you more experience in writing recursive procedures.
Collaboration: You should works in groups of two or three. You may also work alone. You may obtain help from anyone you wish, but you should clearly document that help (perhaps with an extra citations page).
Submitting: Use the ECA.
In our normal pattern of list recursion, we recursively apply a procedure
procedure to the
cdr of the list. However, in some cases,
you might also apply the procedure to the
car of the list.
For example, if we're looking for a value in a list, and that list
contains sublists, we might also want to look in the sublists (which
means that we should recursively call the procedure on the sublists).
Similarly, if any of those sublists contains its own sublist, we will need to recurse on that sub-sublist. And if the sub-sublist contains another list, we'll also need to recurse on that sub-sub-sublist. And so on and so forth.
The variant of recursion that delves into sublists is often called a deep recursion. To some, it is as natural as flat recursion. To others, it seems like a very different strategy.
Here's a sketch of the form of a deep recursive procedure
(define proc (lambda (lst) (cond ((null? lst) base-case) ((list? (car lst)) (combine-1 (proc (car lst)) (proc (cdr lst)))) (else (combine-2 (car lst) (proc (cdr lst)))))))
Document and write the following deep-recursive procedures.
(count-values list), which counts the total number of non-list values in a list (and its sublists, its sub-sublists, and ...).
(deep-tally value list), which counts the number of occurences of value in list (and its sublists, and sub-sublists, and ...).
(deep-filter value list), which removes all occurences of value from list (and its sublists, and sub-sublists, and ...).
(depth list), which determines how deep the deepest value is (a list's depth is one greater than the depth of the deepest sublist; a list with no sublists has depth 1).
(flatten list), which converts a list of arbitrary depth into a list of depth 1. For example,
> (flatten '(((a b) c) d (e (f ((g)))))) (a b c d e f g)
Wednesday, 29 January 2003 [Samuel A. Rebelsky]
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 May 6 09:19:37 2003.
The source to the document was last modified on Wed Jan 29 09:26:27 2003.
This document may be found at