与当今使用的几乎所有其他编程语言中的循环不同LOOP,Common Lisp中的in可以用作表达式:
(let ((doubled (loop for x from 1 to 10 collect (* 2 x)))) doubled) ;; ==> (2 4 6 8 10 12 14 16 18 20) (loop for x from 1 to 10 sum x)
MAXIMIZE导致LOOP返回最大的评估值。MINIMIZE与相对MAXIMIZE。
(loop repeat 100 for x = (random 1000) maximize x)
COUNT告诉您NIL循环中某个表达式对非-计算多少次:
(loop repeat 100 for x = (random 1000) count (evenp x))
LOOP还具有的等同物some,every和notany功能:
(loop for ch across "foobar" thereis (eq ch #\a)) (loop for x in '(a b c d e f 1) always (symbolp x)) (loop for x in '(1 3 5 7) never (evenp x))
...除了它们不限于遍历序列:
(loop for value = (read *standard-input* nil :eof) until (eq value :eof) never (stringp value))
LOOP 产生价值的动词也可以带有-ing后缀:
(loop repeat 100 for x = (random 1000) minimizing x)
也可以将这些动词生成的值捕获到变量中(这些变量由LOOP宏隐式创建),因此您一次可以生成多个值:
(loop repeat 100 for x = (random 1000) maximizing x into biggest minimizing x into smallest summing x into total collecting x into xs finally (return (values biggest smallest total xs)))
你可以有一个以上的collect,count等等条款,收集到相同的输出值。它们将按顺序执行。
以下内容将关联列表(可以与一起使用assoc)转换为属性列表(可以与一起使用getf):
(loop for (key . value) in assoc-list collect key collect value)
尽管这是更好的样式:
(loop for (key . value) in assoc-list append (list key value))