如何减少Common Lisp中的布尔值列表? [英] How does one reduce a list of boolean values in Common Lisp?

查看:78
本文介绍了如何减少Common Lisp中的布尔值列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个值列表,如果所有元素都不为NIL,我想将列表减少为T;如果不是,则要将NIL减少为T.这给了我一个错误:

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))

这是我想出的最好的方法:

This is the best I've come up with:

[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

为什么#"和无效"?在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与ID函数一起用作谓词.

Alternatively you can also use every with the identify function as the predicate.

这篇关于如何减少Common Lisp中的布尔值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆