Tim*_*Tim 5 boolean common-lisp lazy-evaluation
通常and和orCommon Lisp中的运营商将懒洋洋地评估他们的操作数,例如and会一旦它遇到的第一站nil。我正在寻找一个不能以这种方式工作的运算符,而是总是在返回结果之前对所有操作数求值。有这样的东西吗?
例如,在C语言中,您有惰性,&&并且有按位&,可以将其用作非惰性替代。我知道logand和bit-and,但它们不适用于布尔操作数。
例如:
(and NIL (not-defined))
Run Code Online (Sandbox Code Playgroud)
不会抛出错误,但我希望它抛出一个错误。
(defun and* (&rest l)
(every #'identity l))
Run Code Online (Sandbox Code Playgroud)
或如果全部为 true,则返回最后一个值
(defun and& (&rest l)
(or (null l)
(loop for b in l
unless b
do (return nil)
finally (return b))))
Run Code Online (Sandbox Code Playgroud)
或者
(defun and& (&rest l)
(or (null l)
(loop for b in l
always b
finally (return b))))
Run Code Online (Sandbox Code Playgroud)