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))
相关推荐
小七-七牛开发者10 小时前
论文解读:DeepSeek DSpark 在真实高并发推理服务中,如何保证 Token 生成又好又快?
ai·大模型·编程·ai coding
狼爷3 天前
吃透 Java Function 接口,搞定 99% 的 Stream 场景
java·函数式编程
skywalk816315 天前
段言项目推进6.15 @ Dumate+Trae
开发语言·学习·编程
skywalk816315 天前
继续推进心语项目6.15 @CodeArts
开发语言·算法·编程
cup1115 天前
SKILL 第一定律:说点 AI 不知道的
ai·prompt·编程·skill
Tiger Z15 天前
Positron 教程7 --- 工作区
ide·编程·positron
pie_thn16 天前
嵌入式应用开发笔记之web端设备控制台
嵌入式·编程
noipp16 天前
推荐题目:洛谷 P10907 [蓝桥杯 2024 国 B] 蚂蚁开会
c语言·c++·算法·编程·洛谷
Sunsets_Red17 天前
ABC462D 题解
c++·数学·编程·比赛·atcoder·信息学竞赛·信息学