[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: Tue Oct 28 08:34:20 EDT 2008

yes, it works. Thank you. 

-----Message d'origine-----
De : Matthew Flatt [mailto:mflatt at cs.utah.edu] 
Envoyé : October-28-08 8:25 AM
À : andre.mayers at usherbrooke.ca
Cc : plt-scheme at list.cs.brown.edu
Objet : Re: [plt-scheme] I try to understand the use and the nature of pattern variables and identifier in syntax-case

At Mon, 27 Oct 2008 21:08:39 -0400, "Andre Mayers" wrote:
> 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. 

Use `fn' and `ls' inside `syntax' (perhaps using the #' abbreviation)
like this:

 (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 #'fn (stx-car #'ls))
                    (list #'for-each
                          #'fn
                          (stx-cdr #'ls))))]))))


Matthew





Posted on the users mailing list.