sicp每日一题[2.31]

Exercise 2.31

Abstract your answer to Exercise 2.30 to produce a procedure t r e e − m a p tree-map tree−map with the property that s q u a r e − t r e e square-tree square−tree could be defined as

复制代码
(define (square-tree tree) (tree-map square tree))

这道题跟上面一道的 map 实现几乎一模一样,我还以为我理解错题目了,上网搜了一下,发现别人也是这么写的,那就这样吧。

复制代码
(define (tree-map f tree)
  (map (lambda (sub-tree)
         (if (pair? sub-tree)
             (tree-map f sub-tree)
             (f sub-tree)))
       tree))

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


(square-tree
 (list 1
       (list 2 (list 3 4) 5)
       (list 6 7)))

; 执行结果
'(1 (4 (9 16) 25) (36 49))
相关推荐
程序员鱼皮3 天前
67个AI编程必会知识,1.6w字一次讲透!女友:“你要考研啊?!”
ai·程序员·编程·ai编程·vibe coding
明月_清风8 天前
当高阶函数遇到 AI:如何自动化生成业务层面的逻辑拦截器
前端·javascript·函数式编程
明月_清风11 天前
放弃 if-else:学会用 Compose(组合) 将复杂 AI 判别逻辑串成流水线
前端·javascript·函数式编程
小汉堡编程11 天前
LeekCode第3767题选择K个任务的最大总分:详细思考过程幽默解析 专门为小白准备
算法·leetcode·贪心算法·编程·小白专用教程
无名之逆13 天前
你可能不需要WebSocket-服务器发送事件的简单力量
java·开发语言·前端·后端·计算机·rust·编程
锅包一切13 天前
一、C++ 发展与程序创建
开发语言·c++·后端·学习·编程
pie_thn18 天前
这也行?按键动作模式识别也能用贝叶斯?
贝叶斯·编程·模式识别
良许Linux20 天前
FPGA原理和应用
stm32·单片机·fpga开发·程序员·嵌入式·编程