SICP solution exercise 2.31

Solution for exercise 2.31 of SICP:

(define (square x)
  (* x x))

(define (tree-map function tree)
  (map (lambda (subtree)
         (if (pair? subtree)
             (tree-map function subtree)
             (function subtree)))
  tree)) 
  

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

(square-tree
 (list 1
       (list 2 (list 3 4) 5)
       (list 6 7)))
This entry was posted in SICP and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>