如何减少 Common Lisp 中的布尔值列表? [英] How does one reduce a list of boolean values in Common Lisp?
本文介绍了如何减少 Common Lisp 中的布尔值列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个值列表,如果所有元素都不是 NIL,我想将列表减少到 T,否则 NIL.这给了我一个错误:
Given a list of values, I want to reduce the list to T if all the elements are not NIL, NIL if not. This gives me an error:
(apply #'and (get-some-list))
就像这样:
(reduce #'and (get-some-list))
这是我想出的最好的:
[11]> (defun my-and (x y) (and x y))
MY-AND
[12]> (reduce #'my-and '(T T T T T))
T
[13]> (reduce #'my-and '(T T T T NIL))
NIL
为什么#'and"无效?在 Common Lisp 中有没有更惯用的方法来做到这一点?
Why is "#'and" invalid? Is there a more idiomatic way to do this in Common Lisp?
推荐答案
#'and
无效,因为 and
是一个宏,而不是一个函数.
#'and
is invalid because and
is a macro, not a function.
您可以避免使用 lambda 定义命名函数:
You can get around having to define a named function, by using a lambda:
(reduce (lambda (x y) (and x y)) (get-some-list) :initial-value t)
虽然没有像 #'
这样的快捷方式.
There's no shortcut like #'
though.
或者,您也可以将 every
与识别函数一起用作谓词.
Alternatively you can also use every
with the identify function as the predicate.
这篇关于如何减少 Common Lisp 中的布尔值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文