next up previous contents
Next: Using for ... Up: CHAPTER.6: LIST PROCESSING Previous: Predicates on lists

Iterating on lists

The main Pop-11 iteration constructs were given in an earlier chapter. Here we give a number of examples of iteration involving lists. Using iteration we can easily perform a subset of the operations previously illustrated using recursion. Iteration is sometimes easier to understand, and can be more efficient (i.e. easier for the computer). It has the disadvantage that you cannot use TRACE to make explicit what is happening when the program runs.

There are many different forms of iteration over lists. The simplest is a serial scan over the elements of the list. Here is a new definition of TRAVERSE, using an UNTIL loop.

   define traverse(list);
      until list = [] do
        hd(list) =>
        tl(list) -> list
      enduntil;
    enddefine;
    traverse([a b c d]);
    ** a
    ** b
    ** c
    ** d


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