Deep recursion

Exercise 1

Define a procedure count-this-symbol that takes two arguments, the first a list and the second a symbol, and computes and returns the number of occurrences of the specified symbol anywhere inside the given list (including nested lists). (Hint: use count-all-symbols as a pattern.)

Exercise 2

Let's use the term ``tree of symbols'' for a datum like the one used in the preceding examples -- specifically, a list in which each element is either a symbol or another tree of symbols. Define a predicate tree-of-symbols? that takes one argument and returns #t if the argument is a tree of symbols, #f if it is not. For example, (tree-of-symbols? '(a (b (d (e) f)) g (h))) wwould return #t, while (tree-of-symbols? '(i (j (1024)))) would return #f, because it contains 1024, which is not a symbol. (Such a predicate would be useful in adding a precondition test to count-this-symbol.)

Exercise 3

Define a procedure sum-all that takes a tree of numbers -- a list in which each element is either a number or another tree of numbers -- and determines the sum of all the numbers in the tree.

Exercise 4

Trace through the recursive calls resulting (depth '(((a b) c) d (e (f))))?

Exercise 5

Give an example of a tree of symbols of depth 7. Have DrScheme check your answer.

Exercise 6

Define a procedure deep-max that takes a tree of numbers -- a list in which each element is either a number or another tree of numbers -- and returns the largest number in the tree.

Exercise 7

Define a procedure deep-remove-all that takes a nested list as input and returns a list with the same parenthesis structure, but with all of its elements removed. For example, (deep-remove-all '(((a (b)) c) d (e (f)))) would return (((()))(())).

Exercise 8

Define a procedure deep-reverse which takes a tree of symbols tr and returns the reverse of tr with all inner lists reversed as well. For example, (deep-reverse '(a (b (c d) e) f)) would return (f (e (d c) b) a).

Exercise 9

Define a procedure symbols-at-depth that takes two arguments, a tree of symbols tr and a positive integer level, and returns a flat list of the symbols of depth . For example, in the tree of symbols (((a (b)) c) d (e (f))), the nesting level of the symbols c and e is 2, and the rest of the symbols have other nesting levels; so (symbols-at-depth '(((a (b)) c) d (e (f))) 2) should return (c e).

Exercise 10

Define a procedure average-depth that takes a tree of symbols, tr, as an argument, and returns the average depth of the symbols in tr. For example, the average-depth of '(((a (b)) c) d (e (f))) would be (3 + 4 + 2 + 1 + 2 + 3)/6 = 15/6 = 5/2. You may want to use count-all-symbols from the reading as a helper procedure. You will probably also need an additional helper procedure, as well.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~gum/courses/151/labs/deep-recursion.xhtml

created created February 13, 1997
last revised February 7, 2003

John David Stone (stone@cs.grinnell.edu) and Ben Gum (gum@cs.grinnell.edu)