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))
相关推荐
xinxiyinhe2 天前
github免费编程类工具汇总与评估(二)
前端·后端·编程
独泪了无痕3 天前
Optional 使用指南:彻底告别 NPE
后端·函数式编程
HyperAI超神经6 天前
【TVM教程】使用自定义调度规则(Sketch Rule)在 CPU 上自动调度稀疏矩阵乘法
人工智能·深度学习·矩阵·编程·cpu·计算机语言·tvm
skywalk81637 天前
DrRacket是一款专为Scheme和Racket编程语言设计的集成开发环境(IDE)
ide·lisp·drracket
doodlewind8 天前
通过 TypeScript 类型体操学习日语语法
typescript·编程语言·函数式编程
大模型铲屎官9 天前
玩转C#函数:参数、返回值与游戏中的攻击逻辑封装
开发语言·游戏·c#·编程·参数·函数·返回值
网络研究院11 天前
Oracle 公布 Java 的五大新功能
java·oracle·编程·更新·功能
谦谦橘子16 天前
rxjs原理解析
前端·javascript·函数式编程
大模型铲屎官18 天前
Python 科学计算与机器学习入门:NumPy + Scikit-Learn 实战指南
开发语言·人工智能·python·机器学习·numpy·编程·scikit-learn
大模型铲屎官23 天前
Python 性能优化:从入门到精通的实用指南
开发语言·人工智能·pytorch·python·性能优化·llm·编程