乐正

Actions speak louder than words.

Sicp-ex2-31

问题

将你在练习2.30做出的解答进一步抽象,做出一个过程,使它的性质能保证以下面的形式定 义square-tree

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

解答

练习2.31 (ex2-31.scm) download
1
2
3
4
5
6
7
8
;; Happy hacking Yuez - Emacs ♥ you!

(define (tree-map process tree)
  (map (lambda (sub-tree)
         (if (not (pair? sub-tree))
             (process sub-tree)
             (tree-map process sub-tree)))
       tree))

测试

1
2
3
4
5
6
7
8
9
(define (square-tree tree)
  (tree-map square tree))
;Value: square-tree

(square-tree
 (list 1
       (list 2 (list 3 4) 5)
       (list 6 7)))
;Value 16: (1 (4 (9 16) 25) (36 49))

draft

« sicp-ex2-30 sicp-ex2-32 »

Comments