[plt-scheme] I try to understand the use and the nature of pattern variables and identifier in syntax-case

From: Andre Mayers (andre.mayers at usherbrooke.ca)
Date: Mon Oct 27 21:08:39 EDT 2008

Hi, 

As an exercise, I try to define a macro, named for-each. 

(define-syntax for-each
  (λ (stx)
    (let ([stx-ici (quote-syntax ici)])
      (syntax-case stx ()
        [(_ fn ls) 
         (stx-null? (syntax ls))
         #''terminé]
        [(_ fn ls)
           (datum->syntax
             stx-ici
             (list #'begin
                         (list (stx-cadr stx) (stx-car (stx-caddr stx)))
                         (list #'for-each
                               (stx-cadr stx)
                               (stx-cdr (stx-caddr stx)))))]))))

This macro works well but I want to express the result-expression of the
second clause in terms of pattern variables fn and ls. I know that for-each
macro can be programmed more simply the following way 

(define-syntax for-each
  (λ (stx)
     (syntax-case stx ()
        [(_ fn ()) 
         #''terminé]
        [(_ fn (e0 e1 ...))
         (syntax 
          (begin (fn e0)
                 (for-each fn (e1 ...))))])))

however, my purpose is to understand what I can do with pattern variables
and identifiers. 

Below you will find the definition of procedures used in this macro and one
expression using  for-each. 

(require (for-syntax syntax/stx))

(define stx-cadr
  (λ (p)
    (stx-car (stx-cdr p))))

(define stx-caddr
  (λ (p)
    (stx-car (stx-cdr (stx-cdr p)))))

(for-each (lambda (x)
            (display x)
            (newline)) 
          (1 2 3))

any help will be very appreciated. 


André Mayers, Ph.D., M. Ps.
professeur agrégé
Département d'informatique
Université de Sherbrooke
Sherbrooke (Québec) Canada J1K 2R1
tél: +1 819-821-8000 poste 62041
fax: +1 819-821-8200
andre.mayers at usherbrooke.ca
http://www.dmi.usherb.ca/~amayersw.dmi.usherb.ca/~amayers



Posted on the users mailing list.