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))
相关推荐
码农超哥同学8 小时前
Python知识点:如何使用EdgeX Foundry与Python进行边缘计算
开发语言·python·面试·编程·边缘计算
码农超哥同学8 小时前
Python知识点:如何使用OpenFaaS与Python进行无服务器边缘计算
python·面试·serverless·编程·边缘计算
杰哥在此9 小时前
Python知识点:如何使用Hadoop与Python进行大数据处理
开发语言·hadoop·python·面试·编程
码农超哥同学12 小时前
Python知识点:如何使用Google Cloud IoT与Python进行边缘计算
python·物联网·面试·编程·边缘计算
结衣结衣.1 天前
Python基础语法1
开发语言·笔记·python·学习·编程·编程语法
码农超哥同学1 天前
Python知识点:如何使用Edge Impulse与Python进行机器学习模型部署
python·机器学习·面试·edge·编程
a_ran1 天前
一些 Go Web 开发笔记
后端·golang·go·编程·web·网站
请不要叫我菜鸡2 天前
mit6824-01-MapReduce详解
大数据·分布式·后端·mapreduce·函数式编程·容错性
杰哥在此2 天前
Python知识点:如何使用Raspberry Pi与Python进行边缘计算
开发语言·python·面试·编程·边缘计算
码农超哥同学3 天前
Python知识点:如何使用Hive与PyHive进行数据仓库操作
数据仓库·hive·python·面试·编程