Exercise 2.31.  Abstract your answer to exercise 2.30 to produce a procedure tree-map with the property that square-tree could be defined as

(define (square-tree tree) (tree-map square tree))


(define nil '())
(define (tree-map proc tree)
(cond
((null? tree)
nil)
((not (pair? tree))
(proc tree))
(else
(cons
(tree-map proc (car tree))
(tree-map proc (cdr tree))))))
(define (square x) (* x x))
(tree-map square (list 1 (list 2 (list 3 4) 5) (list 6 7))))

view raw

s231.scm

hosted with ❤ by GitHub

 

Discover more from Gaurav Sharma's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading