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))
相关推荐
悦悦子a啊6 小时前
Python之--集合
开发语言·python·编程
songgeb7 天前
Currying and Partial application
swift·函数式编程
程序员鱼皮9 天前
会Vibe Coding的同事:我一个人干掉整个技术部!
ai·程序员·互联网·编程·开发·代码
程序员鱼皮20 天前
Cursor 1.2重磅更新,这个痛点终于被解决了!
ai·程序员·编程·agent·软件开发
开开心心就好1 个月前
高效批量转换Word到PDF的方法
javascript·安全·智能手机·pdf·word·objective-c·lisp
qq_428639611 个月前
编程基础:调用&访问
编程
在未来等你1 个月前
Java企业技术趋势分析:AI驱动下的Spring AI、LangChain4j与RAG系统架构
java·spring·ai·编程·技术
桦说编程1 个月前
写时复制COW核心原理解读
java·性能优化·函数式编程
程序员鱼皮1 个月前
突发,小红书开发者后门被破解?!
计算机·程序员·编程·事故
桦说编程1 个月前
配置快照实现之持久化数据结构
java·后端·函数式编程