A better version of subst reuses parts of the original tree when possible:
(define (subst new old tree) (if (pair? tree) (let ((left (subst new old (car tree))) (right (subst new old (cdr tree)))) (if (and (eq? left (car tree)) (eq? right (cdr tree))) tree (cons left right))) (if (eqv? old tree) new tree)))
This is a good example of use of eq? to test for identical pairs. The eq? test will be true when no substitutions were made in part of the tree; in this case, that subtree can be shared and reused in the new structure.
Contents    Page-10    Prev    Next    Page+10    Index