chickadee » foof-loop » IN-LIST

(IN-LIST <list> [<successor>])syntax

Usage:

   (FOR <element> [<pair>] (IN-LIST <list> [<successor>]))

Iterates for each successive pair in <list>, binding the variable <pair>, if supplied, to that pair, and the variable <element> to the car of that pair. The successor of the pair is taken by applying <successor> to the current pair. The iteration runs out when the successor is no longer a pair.

<Successor>, if supplied, must be a unary procedure. The default value of <successor> is CDR. <List> and <successor> are evaluated once before the loop begins.

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

<Element> is a body variable.

The next pair is taken before the loop body, so the loop body may reliably apply SET-CDR!, or its equivalent for the given successor procedure, to the pair, without altering the iteration. Note, though, that <successor> will be called irrespective of whether <pair> is updated explicitly.

   (loop ((for a (in-list '(a b c)))
          (for b (in-list '(p q r))))
     (write (list a b))
     (newline))
   ;; Output:
   ;(a p)
   ;(b q)
   ;(c r)
   
   (loop ((for x (in-list '(1 2 3)))
          (with y 0 (+ y (* x 2))))
     => y)
   ;Value: 12
   
   ;;; PAIR-FOLD from SRFI 1.
   
   (define (pair-fold kons knil list)
     (loop ((for elt pair (in-list list))
            (with knil knil (kons pair knil)))
       => knil))