next up previous contents
Next: Iteration vs Recursion Up: CHAPTER.6: LIST PROCESSING Previous: Using for ...

Iterating over two or more lists

if list1, list2, list3, ... listN, are lists, then it is possible to use N variables and iterate over all the N lists at once, thus:

    for item1, item2, ... itemN in list1, list2, ... listN do
        <instructions using item1, item2, ...itemN >
    endfor
The instructions will be obeyed N times, each time taking one item from each list. If any lists are shorter than the shortest list, then their trailing items will be ignored.

E.g.

    vars item1, item2, item3;
    for item1, item2, item3 in
        [the every each all some],
        [men pig cow cars],
        [stank slept fell ate]
    do
        [^item1 ^item2 ^item3] =>
    endfor;
    ** [the men stank]
    ** [every pig slept]
    ** [each cow fell]
    ** [all cars ate]


Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998