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

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Oct 28 08:24:32 EDT 2008

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.