# SICP Solutions

### Section - 2.2 - Hierarchical Data and the Closure Property

#### Exercise 2.35

Number of leaves in a tree, $t$, is:

• if $t$ is leaf, $num\_of\_leaves(t) = 1$.
• else
$num\_of\_leaves(t) = \sum_{ b \in (\text{ all branches of } t) } num\_of\_leaves(b)$.

The above approach can be directly translated into the following procedure:

As we can see, accumulate maps to $\sum$ and $num\_of\_leaves$ maps to count-leaves.

But it does not fit with the exercise, as it expects to use map:

Here, map is used to convert(maps) each branch of the tree passed to it to(into) number of leaves. And procedure accumulate computes total number of leaves by adding the number of leaves of each branch.

Procedure count-leaves-orig is the recursive procedure for counting leaves given in book.

Example/Output: