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 小时前
23 openclaw防止SQL注入:参数化查询与ORM安全使用
数据库·sql·安全·ai·编程·技术
marsh020620 小时前
22 openclaw身份认证与授权:构建安全的访问控制
安全·ai·编程·技术
乐茵lin1 天前
大厂都在问:如何解决map的并发安全问题?三种方法让你对答如流
开发语言·go·编程·map·并发安全·底层源码·sync.map
程序员鱼皮2 天前
315 曝光的 GEO 投毒是什么?教你 8 招,让 AI 主动推荐你!
ai·程序员·编程·ai编程·seo
marsh02062 天前
21 openclaw安全威胁模型:识别常见攻击向量
安全·ai·编程·技术
爱思德学术4 天前
IEEE会议,录用率25.2%!CCF推荐学术会议(C)
计算机网络·算法·编程·软件工程·软件需求
biuba10244 天前
18 openclaw事务管理:确保数据一致性的最佳实践
开发语言·ai·c#·编程·技术
程序员鱼皮4 天前
我的免费 OpenClaw 零基础教程,爆了!
ai·程序员·编程·ai编程·openclaw
henreash4 天前
函数式编程的架构目标
函数式编程
Jay-r5 天前
樱花雨特效 WebGL实现 短视频同款浪漫视觉效果(附源码下载)
开发语言·javascript·ecmascript·编程·webgl·代码·樱花雨