灯下 登录
计算机科学 / SICP / 2.2.2 Hierarchical Structures

Exercise 2.30 · 习题

Exercise 2.30: Define a procedure
square-tree analogous to the square-list procedure of
Exercise 2.21. That is, square-tree should behave as follows:

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

Define square-tree both directly (i.e., without using any higher-order

procedures) and also by using map and recursion.

练习 2.30:定义一个过程 square-tree,使其类似于练习 2.21 中的 square-list 过程。也就是说,square-tree 的行为应如下所示:

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

请直接定义 square-tree(即不使用任何高阶过程),以及用 map 和递归来定义它。

Racket #lang sicp
(square-tree
 (list 1
 (list 2 (list 3 4) 5)
 (list 6 7)))
(1 (4 (9 16) 25) (36 49))