当前位置: 首页 > news >正文

sicp每日一题[2.30]

Exercise 2.30

Define a procedure s q u a r e − t r e e square-tree squaretree analogous to the s q u a r e − l i s t square-list squarelist procedure of Exercise 2.21. That is, s q u a r e − t r e e square-tree squaretree should behave as follows:

(square-tree(list 1(list 2 (list 3 4) 5)(list 6 7)))
(1 (4 (9 16) 25) (36 49))

Define s q u a r e − t r e e square-tree squaretree both directly (i.e., without using any higher-order procedures) and also by using map and recursion.


这个题目没啥难度,直接仿照 scale-tree 函数,修改一下函数名就行了。

(define (square-tree tree)(cond ((null? tree) nil)((not (pair? tree)) (square tree))(else (cons (square-tree (car tree))(square-tree (cdr tree))))))(define (square-tree-by-map tree)(map (lambda (sub-tree)(if (pair? sub-tree)(square-tree-by-map sub-tree)(square sub-tree)))tree))(define test (list 1(list 2 (list 3 4) 5)(list 6 7)))(square-tree test)
(square-tree-by-map test); 执行结果
'(1 (4 (9 16) 25) (36 49))
'(1 (4 (9 16) 25) (36 49))

http://www.mrgr.cn/news/37255.html

相关文章:

  • Java中的注解处理器:自定义注解与APT工具的应用场景
  • YOLOv8默认使用的优化器是什么?optimizer默认值auto指定的是什么优化器?
  • 【玩转贪心算法专题】738. 单调递增的数字【中等】
  • 【Gitee自动化测试2】Git,TortoiseGit,Github,Gitlab,Gitee
  • 心觉:如何重塑高效学习的潜意识(3)东西很好,但用不起,怎么破?
  • C++学习笔记----8、掌握类与对象(一)---- 对象中的动态内存分配(4)
  • 【项目经验分享】深度学习点云算法毕业设计项目案例定制
  • 【机器学习】探索LSTM:深度学习领域的强大时间序列处理能力
  • Mysql InnoDB引擎的四大核心特性
  • 衡石分析平台系统管理手册-功能配置之全局 CSS 设置
  • Github 2024-09-27 Java开源项目日报Top8
  • python-list-comprehension-find-pair-given-sum-two-arrays
  • 【Python报错已解决】TypeError: tuple indices must be integers or slices, not str
  • 九、子查询
  • 在实时语音交互上超过GPT-4o,端到端语音模型Mini-Omni部署
  • 使用Keras进行图像分类:从入门到精通
  • Deno 1.46
  • 1.6 判定表
  • 网页制作Dreamweaver CC2024集成AI
  • Unity3D地形系统一口气讲完!谢啦!!☆⌒(*^-゜)v