Next: ’for-as-on-list’, Previous: ’for-as-arithmetic’, Up: Iteration Control [Contents][Index]
This is the second of seven ‘for/as’ syntaxes.
for var [type-spec] in expr1 [by step-fun] as var [type-spec] in expr1 [by step-fun]
This construct iterates over the contents of a list.
endp.
var is bound to the successive elements of the list expr1
before each iteration.
step-fun is called on the list
and is expected to produce a successor list; the default value for
step-fun is the cdr function.
;;; Print every item in a list. (loop for item in '(1 2 3 4 5) do (print item)) ;Prints 5 lines
;;; Print every other item in a list.
(loop for item in '(1 2 3 4 5) by #'cddr
do (print item)) ;Prints 3 lines
;;; Destructure items of a list, and sum the x values
;;; using fixnum arithmetic.
(loop for (item . x) (t . fixnum)
in '((A . 1) (B . 2) (C . 3))
unless (eq item 'B) sum x)