define-syntax for do 
Author Message
 define-syntax for do

Could someone send me or point me to (define-syntax ...) code which covers all
cases of R4RS "do"?

Thanks,
Harold



Sun, 27 Jun 1999 03:00:00 GMT  
 define-syntax for do


Quote:

>Could someone send me or point me to (define-syntax ...) code which covers all
>cases of R4RS "do"?

I believe this does the right thing.  This code is takes from the
current Kawa snapshot (ftp://ftp.cygnus.com/pub/bothner/kawa-1.1alpha.tar.gz).

;;; Helper macro for do, to handle optional step.
(define-syntax %do-step (syntax-rules ()
                                      ((%do-step variable step) step)
                                      ((%do-step variable) variable)))

(define-syntax do (syntax-rules ()
                                ((do ((variable init . step) ...)
                                     (test sequence) commands ...)
                                 (letrec ((loop
                                           (lambda (variable ...)
                                             (if test
                                                 (begin sequence)
                                                 (begin
                                                   commands ...
                                                   (loop (%do-step variable
                                                          . step) ...))))))
                                   (loop init ...))
                                 )))
--
        --Per Bothner



Tue, 29 Jun 1999 03:00:00 GMT  
 define-syntax for do



   >Could someone send me or point me to (define-syntax ...) code which covers all
   >cases of R4RS "do"?

   ;;; Helper macro for do, to handle optional step.
   (define-syntax %do-step (syntax-rules ()
                                         ((%do-step variable step) step)
                                         ((%do-step variable) variable)))

   (define-syntax do (syntax-rules ()
                                   ((do ((variable init . step) ...)
                                        (test sequence) commands ...)
                                    (letrec ((loop
                                              (lambda (variable ...)
                                                (if test
                                                    (begin sequence)
                                                    (begin
                                                      commands ...
                                                      (loop (%do-step variable
                                                             . step) ...))))))
                                      (loop init ...))
                                    )))

This requires exactly one <sequence> form.  My understanding of R4RS is
that zero or more should be allowed.  For example:

(define-syntax do
  (syntax-rules ()
    ((do ((?name ?init . ?step) ...)
         (?test . ?result)
       ?body ...)
     (let-syntax ((do-step (syntax-rules ()
                             ((do-step ?n) ?n)
                             ((do-step ?n ?s) ?s)))
                  (do-result (syntax-rules ()
                               ((do-result) (if #f #f))
                               ((do-result . ?r) (begin . ?r)))))
       (let loop ((?name ?init) ...)
         (if ?test
             (do-result . ?result)
             (begin ?body ...
                    (loop (do-step ?name . ?step) ...))))))))

Or you could just hand the entire clause off to COND.  This macro
(incidentally) works without LET-SYNTAX or auxiliary macros, but is
certainly not the most efficient way of doing things.

(define-syntax do
  (syntax-rules ()
    ((do ((?name ?init ?step) ...)
         ?exit-clause
       ?body ...)
     (let loop ((?name ?init) ...)
       (cond ?exit-clause
             (else ?body ... (loop ?step ...)))))
    ((do ((?name ?init) ?binding ...) . ?rest)
     (do (?binding ... (?name ?init ?name)) . ?rest))
    ((do ((?name ?init ?step) ?binding ...) . ?rest)
     (do (?binding ... (?name ?init ?step)) . ?rest))))



Wed, 30 Jun 1999 03:00:00 GMT  
 define-syntax for do

People have posted out tha my implementation of do did not support
other than one form in the result.  Based on posted suggestions,
I humbly submit the following.  Anyone see anything wrong with it?
(Also, does anyone have a Scheme test suite more comprehensive
than Jaffar's test.scm?)

(define-syntax %do-step
  (syntax-rules ()
                ((%do-step variable step) step)
                ((%do-step variable) variable)))

(define-syntax do
  (syntax-rules ()
                ((do ((name init . step) ...)
                     (test . result) commands ...)
                 (letrec ((loop
                           (lambda (name ...)
                             (if test
                                 (begin (if #f #f) . result)
                                 (begin commands ...
                                        (loop (%do-step name . step) ...))))))
                   (loop init ...)))))
--
        --Per Bothner



Mon, 05 Jul 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. is define-syntax inside define-syntax OK?

2. shadows of define and define-syntax

3. define in define-syntax

4. define-macro -> define-syntax

5. define-macro vs define-syntax

6. Macros defining macros with define-syntax

7. define-syntax must be followed by syntax-rules ??

8. unusual define-syntax syntax

9. Alomst done, NASM syntax...

10. Syntax Error: Label in prototype not defined SECURITY_ATTRIBUTES.

11. define-syntax problem...

12. define-syntax

 

 
Powered by phpBB® Forum Software