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))
相关推荐
marsh02061 天前
47 openclaw监控指标设计:关键性能指标(KPI)选择与实现
网络·ai·编程·技术
skywalk81631 天前
全面评估这门中文语言的情况,看它离一个可以实际产业落地的编程语言还有多远距离!
开发语言·编程
小贺儿开发2 天前
Unity3D 编辑器对象锁定工具
unity·编辑器·编程·工具·对象·互动·拓展
skywalk81632 天前
zhixing 知行中文编程语言开发@CodeArts
python·编程
Tiger Z3 天前
Positron 教程1 --- 用户界面
ide·编程·positron
Json____3 天前
Python练习题集-文件处理、数据管理与网络编程实战小项目15个
python·编程·编程学习·练习题·python学习
nuowenyadelunwen4 天前
CS 61A Lab 2 笔记:短路求值、高阶函数与 Lambda 表达式
python·函数式编程·cs61a·berkeley
zhangfeng11335 天前
CodeBuddy ai对话框上面的git docs terminal Rulds 干嘛用的,以thinkphp fastadmin 为例,插件市场
人工智能·git·编程
程序员鱼皮5 天前
再见百度,我用 1 小时,开发了个 AI 搜索引擎!Codex + GPT 5.5 + DeepSeek V4 真香~
计算机·ai·程序员·编程·ai编程