5.3.1. Memory as Vectors
Exercise 5.21

Register machines for count-leaves

Implement register machines for the following procedures. Assume that the list-structure memory operations are available as machine primitives.

(define (count-leaves tree)
  (cond ((null? tree) 0)
        ((not (pair? tree)) 1)
        (else (+ (count-leaves (car tree))
                 (count-leaves (cdr tree))))))

a. Recursive count-leaves:

(define (count-leaves tree)
  (define (count-iter tree n)
    (cond ((null? tree) n)
          ((not (pair? tree)) (+ n 1))
          (else (count-iter (cdr tree)
                            (count-iter (car tree) n)))))
  (count-iter tree 0))

b. Recursive count-leaves with explicit counter:


Nobody's finished this exercise yet. You'll be the first!


There are no comments yet.

Authentication required

You must log in to post a comment.

Login