common-lisp 并行迭代
示例
FOR允许使用多个子句LOOP。这些子句中的第一个完成时,循环结束:
(loop for a in '(1 2 3 4 5)for b in '(a b c)
collect (list a b))
;; Evaluates to: ((1 a) (2 b) (3 c))
可以组合其他确定循环是否应该继续的子句:
(loop for a in '(1 2 3 4 5 6 7)while (< a 4)
collect a)
;; Evaluates to: (1 2 3)
(loop for a in '(1 2 3 4 5 6 7)
while (< a 4)
repeat 1
collect a)
;; Evaluates to: (1)
确定哪个列表更长,并在知道答案后立即中断迭代:
(defun longerp (list-1 list-2)(loop for cdr1 on list-1
for cdr2 on list-2
if (null cdr1) return nil
else if (null cdr2) return t
finally (return nil)))
为列表中的元素编号:
(loop for item in '(a b c d e f g)for x from 1
collect (cons x item))
;; Returns ((1 . a) (2 . b) (3 . c) (4 . d) (5 . e) (6 . f) (7 . g))
确保列表中的所有数字均为偶数,但仅适用于前100个项目:
(assert(loop for number in list
repeat 100
always (evenp number)))
以上是 common-lisp 并行迭代 的全部内容, 来源链接: utcz.com/z/351353.html