Lab 4

Goals

1. To write a procedure that traverses a list.

2. To write a procedure that traverses (a list representation of) a tree.

Experiments

1. Define a procedure called number-of-occurrences that takes two arguments. The first argument is a number, and the second argument is a list of numbers. Define number-of-occurrences so that it returns the number of times that the number (first argument) occurs in the list (second argument). The numbers in the list can be in any order, and obviously repetitions are allowed. Thus, a procedure call like this:
     (number-of-occurrences 5 (list 2 5 7 6 5 4 3 5 2 7 6 8 5 3))
will return 4. If the number does not appear at all in the list, 0 will be returned.

2. Define a procedure leaf-in-tree? that takes two arguments. The first argument is a number, and the second argument is a list representation of a tree, all of whose leaves are numbers. If the number (first argument) appears anywhere as a leaf in the tree, leaf-in-tree? returns #t; otherwise, it returns #f. For example, if

     (define x (list (list 3 5) (list (list 2 5) 3) 6 (list (list 4))))
     (leaf-in-tree? 5 x)
     (leaf-in-tree? 7 x)
is executed, the value of x will be ((3 5) ((2 5) 3) 6 ((4))), the call to (leaf-in-tree? 5 x) will return #t and the call to (leaf-in-tree? 7 x) will return #f.

Summit your definitions of these two procedures to the Automated Tester and click on the Confirm butten when you get a PASS.