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))
相关推荐
吴可可1231 天前
多段线整体弯曲LISP实现
lisp
吴可可1232 天前
查找最近多段线的LISP实现
lisp
吴可可1232 天前
Lisp函数定义四法详解
lisp
aluluka2 天前
《elisp 每日一练》——Emacs的启动加速
emacs·lisp·elisp
吴可可1233 天前
LISP实现多段线精准三段打断
lisp
codigger5 天前
同一个端口能不能被两个程序同时监听?
java·后端·程序员·编程
codigger8 天前
Vue nextTick 到底在等什么?一篇讲透原理与那些年踩过的坑
程序员·vue·编程·异步更新·#人工智能
aluluka11 天前
《elisp 每日一练》——定时器与进程调用综合练习
emacs·lisp·elisp
codigger11 天前
别再靠"最终版-改"活下去了:一篇讲透 Git 与 GitHub
git·程序员·github·编程·控制版本
aluluka11 天前
《elisp 每日一练》——Process Sentinel 与 json集成
json·emacs·lisp·elisp