chickadee » foof-loop » IN-LISTS

(IN-LISTS <lists> [<tail>])syntax

Usage:

 (FOR <elements> [<pairs>] (IN-LISTS <lists> [<tail>]))

Iterates for each successive pair in each parallel element of <lists>, binding the variable <pairs>, if supplied, to be a list of those pairs, and the variable <elements> to a list of the cars of those pairs, prepended to the value of <tail> evaluated at each iteration.

no <tail> is supplied, its default value is the empty list.  The

successor of each pair is taken by CDR. The iteration runs out when any successor is no longer a pair.

<Lists> must be a non-empty list; it is an error if otherwise. It is evaluated once before the loop begins.

<Pairs> is a loop variable; its value in the final expression is unspecified.

<Elements> is a body variable.

The cdrs are taken before the loop body, so the loop body may reliably apply SET-CDR! to any of the pairs without altering the iteration.

   ;;; Transpose a matrix represented as a nested list.
   
   (loop ((for columns (in-lists '((a b c) (d e f))))
          (with rows '() (cons columns rows)))
     => rows)
   ;Value: ((c f) (b e) (a d))
   
   (define (every? predicate list . lists)
     (loop proceed ((for elts (in-lists (cons list lists))))
       (and (apply predicate elts)
            (proceed))))
   
   (define (any predicate list . lists)
     (loop proceed ((for elts (in-lists (cons list lists))))
       (or (apply predicate elts)
           (proceed))))
   
   ;;; SRFI 1's signature for FOLD.  Notice that KNIL is the *last*
   ;;; argument to the FOLD procedure, so we have to tack it on to the
   ;;; end of the argument list.
   
   (define (fold kons knil list . lists)
     (loop ((with knil knil (apply kons arguments))
            (for arguments 
                 (in-lists (cons list lists)
                           (cons knil '()))))
       => knil))