
2.9 Advanced iteration
using allows you to name this internal index as well as the internal variable that represents the sequence; it has the following syntax: {for|as} var [type-spec] being {each|the} loop-method-name
[in|of]
expression using [(index var)] [(sequence var)]
During each iteration, the loop keywordusing directs the Loop Facility to use named variables for the values of the variables marked by the loop keywordsindex andsequence. The user can then access the index or the sequence inside the loop, as the following examples show:
;; Print the elements and the indices of a string vector.
> (loop for x being the elements of "fun" using (index i)
do (print x) (print i))
#\f
0
#\u
1
#\n
2
NIL
;; Collect the indices and elements of a list.
> (loop for x being the elements of '(a b c d e f g)
using (index i)
collect (list i x))
((0 A) (1 B) (2 C) (3 D) (4 E) (5 F) (6 G))
;; Collect the entire sequence, and then collect everything but
;; the first element of the sequence.
> (defun compute-my-string ()
"The cat purred")
> (loop for c being the elements of (compute-my-string)
using (sequence s)
for x first s then c
collect x)
("The cat purred" #\h #\e #\Space #\c #\a #\t #\Space #\p #\u #\r #\r #\e #\d)

Generated with Harlequin WebMaker