Common Lisp:列表中的所有或任何元素都是真的

Mir*_*lov 15 lisp python list common-lisp

在Python中有函数all,any如果列表的全部或部分元素分别为true,则它们返回true.Common Lisp中是否有相同的功能?如果没有,那么最简洁和惯用的方式是什么?

目前我有这个:

(defun all (xs)
  (reduce (lambda (x y) (and x y)) xs :initial-value t))

(defun any (xs)
  (reduce (lambda (x y) (or x y)) xs :initial-value nil))
Run Code Online (Sandbox Code Playgroud)

Ósc*_*pez 29

在Common Lisp中,使用每个(相当于all)和一些(相当于any).


Rai*_*wig 6

你可以使用LOOP宏和ALWAYS和这样的THEREIS子句:

CL-USER 1 > (loop for item in '(nil nil nil) always item)
NIL

CL-USER 2 > (loop for item in '(nil nil t) always item)
NIL

CL-USER 3 > (loop for item in '(t t t) always item)
T

CL-USER 4 > (loop for item in '(nil nil nil) thereis item)
NIL

CL-USER 5 > (loop for item in '(nil nil t) thereis item)
T

CL-USER 6 > (loop for item in '(t t t) thereis item)
T
Run Code Online (Sandbox Code Playgroud)