<< Previous exercise (2.30) | Index | Next exercise (2.32) >>
my way:
(define (tree-map proc tree)
(cond ((null? tree) nil)
((pair? tree)
(cons
(tree-map proc (car tree))
(tree-map proc (cdr tree))))
(else (proc tree))))
my way. For me this is more succinct than using cond and there's no need to check for nil explicitly (or define it):
(define (tree-map proc tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(tree-map proc sub-tree)
(proc sub-tree)))
tree))
Pretty nice.