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))
相关推荐
云空2 天前
《完整开源魔方项目分类清单(按用途/语言划分,含GitHub地址、核心能力、适用场景)》
开源·编程·魔方
码字的特恩2 天前
微软确认暂不为 Windows 11 加入透明效果自定义功能,建议用户使用第三方工具
人工智能·windows·算法·microsoft·计算机·大模型·编程
云空2 天前
《软件专业完整学习路线图(本科4年+自学通用版,2026就业向)》
人工智能·科技·学习·计算机·编程·软件
noipp11 天前
推荐题目:洛谷 P3726 [AHOI2017/HNOI2017] 抛硬币
c语言·数据结构·c++·算法·编程·洛谷·luogu
码字的特恩13 天前
GPT-5.6自己优化自己实锤了,新的左脚踩右脚已经出现
人工智能·gpt·深度学习·算法·大模型·互联网·编程
Jay-r13 天前
手势粒子特效系统 Gesture Particle FX(附源码下载)
python·ai·编程·pygame·百度云·手势控制
大模型momo13 天前
AI 编程工程化:从 Prompt 到 Harness
人工智能·prompt·编程·agent
码字的特恩15 天前
AI Agent Skill 工程化 09:让 Skill 自己变好——走向自进化流水线
人工智能·计算机·ai·程序员·大模型·互联网·编程
人间凡尔赛15 天前
2026年AI编程新范式:从Copilot到Agentic Coding的实战指南
ai·编程·agent·工具·效率
osbxbv_47116 天前
消息中间件选型对比分析
编程