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))
相关推荐
xp4758063805 天前
陪诊公司是什么?绿通在北京陪诊服务中的作用是什么?
游戏·编程
酬勤-人间道5 天前
XPlote3DGenie 2.1.1.0:实用 3D 数据处理工具,百度网盘可直接安装
c++·3d·gis·编程·计算机软件·岩土
十年编程老舅7 天前
虾皮C++一面:C++四种类型转换详解
程序员·编程·c/c++
xp4758063808 天前
当选择北京陪诊公司时,如何找到靠谱的陪诊服务?
游戏·编程
程序员鱼皮9 天前
20 个神级 AI 编程扩展,爽爆了!
ai·程序员·编程
开开心心_Every9 天前
文件数量统计工具:支持多层文件夹数量统计
游戏·微信·pdf·excel·语音识别·swift·lisp
王老师青少年编程13 天前
2025年3月GESP真题及题解(C++七级): 等价消除
c++·编程·题解·真题·gesp·七级·等价消除
王老师青少年编程13 天前
2025年3月GESP真题及题解(C++八级): 上学
c++·编程·题解·真题·gesp·八级·上学
Tiger Z14 天前
《R for Data Science (2e)》免费中文翻译 (第17章) --- Dates and times(2)
r语言·编程·数据科学
CS创新实验室18 天前
AI 与编程
人工智能·编程·编程语言