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)
你可以使用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)
| 归档时间: |
|
| 查看次数: |
1825 次 |
| 最近记录: |