Next: , Previous: , Up: Iteration Control   [Contents][Index]


15.25.6.3 ’for-as-in-list’

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]

List Iteration

This construct iterates over the contents of a list.

;;; 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)