为什么 (not? (pair? x) [...]) 优于 ((pair? x) [...])?
Why is (not? (pair? x) [...]) beneficial over ((pair? x) [...])?
在 SICP 的第 2 章中,特别是 section 2.2. 及其在 Scheme wiki 上的相应解决方案,我在处理树结构时经常看到相同的模式:
(cond ((null? tree) nil)
((not (pair? tree)) (foo tree))
(else (cons (bar (car tree))
(bar (cdr tree)))))
这些结构的第二行经常让我感到困惑。在任何其他语言中,我希望看到最后两个分支交换如下:
(cond ((null? tree) nil)
((pair? tree) (cons (bar (car tree))
(bar (cdr tree))))
(else (foo tree)))
为什么我在 Scheme 中看不到它?与交换分支和删除 not?
.
相比,更喜欢 (not? (pair? x) [...])
有什么好处吗?
案例通常从最小或最特殊的案例开始,到最大或最一般的案例结束。
如果是一棵树,那就是
- 空
- 叶子
- 所有其他树
在 SICP 的第 2 章中,特别是 section 2.2. 及其在 Scheme wiki 上的相应解决方案,我在处理树结构时经常看到相同的模式:
(cond ((null? tree) nil)
((not (pair? tree)) (foo tree))
(else (cons (bar (car tree))
(bar (cdr tree)))))
这些结构的第二行经常让我感到困惑。在任何其他语言中,我希望看到最后两个分支交换如下:
(cond ((null? tree) nil)
((pair? tree) (cons (bar (car tree))
(bar (cdr tree))))
(else (foo tree)))
为什么我在 Scheme 中看不到它?与交换分支和删除 not?
.
(not? (pair? x) [...])
有什么好处吗?
案例通常从最小或最特殊的案例开始,到最大或最一般的案例结束。
如果是一棵树,那就是
- 空
- 叶子
- 所有其他树