函数收集满足谓词的子树的标准名称?
Standard name for function collecting subtrees satisfying a predicate?
在我编写的几乎每个 Lisp 程序中,我都不得不重新实现一个特定的函数。既然这个功能这么好用,那肯定是之前实现过的。我希望它会广为人知。也许它是 Common Lisp 标准库的一部分。它叫什么,来自哪个图书馆?
(defun unknown-function (predicate tree)
(loop for item in tree
if (funcall predicate item) collect item
else if (listp item) append (unknown-function predicate item)))
它向下遍历一棵树,并创建该树中满足谓词的所有节点的平面列表。
我原来的陈述是错误的,因为子列表在下降到之前由谓词测试的微妙之处。在这里,为了后代:
There's no standard name for this. It's just a combination of flattening a list of lists and filtering out the elements that don't satisfy a predicate. In Common Lisp, there's no built-in flatten, but it would be a combination of your own flatten, and the standard remove-if-not.
这与 subst 系列函数有更多共同之处,除了检查叶子之外,还检查子树。但是,他们正在替换树的各个元素,而不是完全删除它们。所以 subst-if 和 subst-if-not 有一些共同点,但它们仍然不是完全匹配。
在我编写的几乎每个 Lisp 程序中,我都不得不重新实现一个特定的函数。既然这个功能这么好用,那肯定是之前实现过的。我希望它会广为人知。也许它是 Common Lisp 标准库的一部分。它叫什么,来自哪个图书馆?
(defun unknown-function (predicate tree)
(loop for item in tree
if (funcall predicate item) collect item
else if (listp item) append (unknown-function predicate item)))
它向下遍历一棵树,并创建该树中满足谓词的所有节点的平面列表。
我原来的陈述是错误的,因为子列表在下降到之前由谓词测试的微妙之处。在这里,为了后代:
There's no standard name for this. It's just a combination of flattening a list of lists and filtering out the elements that don't satisfy a predicate. In Common Lisp, there's no built-in flatten, but it would be a combination of your own flatten, and the standard remove-if-not.
这与 subst 系列函数有更多共同之处,除了检查叶子之外,还检查子树。但是,他们正在替换树的各个元素,而不是完全删除它们。所以 subst-if 和 subst-if-not 有一些共同点,但它们仍然不是完全匹配。