Common Lisp中常用的and
和or
运算符会延迟计算其操作数,例如一旦遇到第一个and
,nil
就会停止。我正在寻找一个不能以这种方式工作的运算符,而是总是在返回结果之前对所有操作数求值。有这样的东西吗?
例如,在C语言中,您有惰性&&
和按位的&
,它们可以用作非惰性替代。
我知道logand
和bit-and
,但是它们不适用于 boolean 操作数。
例如。:
(and NIL (not-defined))
不会抛出错误,但是我希望它抛出一个错误。
最佳答案
(defun and* (&rest l)
(every #'identity l))
或返回最后一个值(如果全部为true)
(defun and& (&rest l)
(or (null l)
(loop for b in l
unless b
do (return nil)
finally (return b))))
或者
(defun and& (&rest l)
(or (null l)
(loop for b in l
always b
finally (return b))))
关于boolean - Common Lisp中是否有非延迟评估的 "and"或 "or"操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478184/