Previous: Defining sequence types, Up: Macros for writing loops


6.3.6 Loop macro expansion

Here is an example of the expansion of the reduce macro:

     (reduce ((list* x '(1 2 3)))
         ((r '()))
       (cons x r))
         ==>
     (let ((final (lambda (r) (values r)))
           (list '(1 2 3))
           (r '()))
       (let loop ((list list) (r r))
         (if (null? list)
             (final r)
             (let ((x    (car list))
                   (list (cdr list)))
               (let ((continue (lambda (r)
                                 (loop list r))))
                 (continue (cons x r)))))))

The only mild inefficiencies in this code are the final & continue procedures, both of which could trivially be substituted in-line. The macro expander could easily perform the substitution for continue when there is no explicit proceed variable, as in this case, but not in general.