learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-31.scm (636B)


      1 ;; Exercise 2.31
      2 
      3 (define (exercise-2.31)
      4   (define (tree-map proc tree)
      5     (map (lambda (sub-tree)
      6            (if (pair? sub-tree)
      7                (tree-map proc
      8                          sub-tree)
      9                (proc sub-tree)))
     10          tree))
     11 
     12   (define (square-tree tree)
     13     (tree-map (lambda (x) (* x x))
     14               tree))
     15 
     16   (define a-tree
     17     '(1 (2 (3 4) 5)
     18         (6 7)))
     19 
     20   (define a-tree-squared
     21     '(1 (4 (9 16) 25)
     22         (36 49)))
     23 
     24   (test-begin "2.31")
     25   (test-equal
     26       a-tree-squared
     27     (square-tree a-tree))
     28   (test-equal
     29       a-tree-squared
     30     (square-tree a-tree))
     31   (test-end "2.31"))
     32 
     33 (exercise-2.31)