learning-sicp

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

exercise-30.scm (796B)


      1 ;; Exercise 2.30
      2 
      3 (define (exercise-2.30)
      4   (define (directly-square-tree tree)
      5     (cond
      6      ((null? tree) '())
      7      ((pair? tree) (cons (directly-square-tree (car tree))
      8                          (directly-square-tree (cdr tree))))
      9      (else (* tree tree))))
     10 
     11   (define (higher-order-square-tree tree)
     12     (map (lambda (sub-tree)
     13            (if (pair? sub-tree)
     14                (higher-order-square-tree sub-tree)
     15                (* sub-tree sub-tree)))
     16          tree))
     17 
     18   (define a-tree
     19     '(1 (2 (3 4) 5)
     20         (6 7)))
     21 
     22   (define a-tree-squared
     23     '(1 (4 (9 16) 25)
     24         (36 49)))
     25 
     26   (test-begin "2.30")
     27   (test-equal
     28       a-tree-squared
     29     (directly-square-tree a-tree))
     30   (test-equal
     31       a-tree-squared
     32     (higher-order-square-tree a-tree))
     33   (test-end "2.30"))
     34 
     35 (exercise-2.30)