[plt-scheme] Best practices when writing macros; avoid name shadowing?

From: Grant Rettke (grettke at acm.org)
Date: Sun Feb 3 23:39:01 EST 2008

On Feb 3, 2008 10:09 PM, Chongkai Zhu <czhu at cs.utah.edu> wrote:
> Have you ever heard "hygienic macro" before? If not, google.

Yes! :)

I am using syntax-case.

Now I am even reading up on it too before bothering you with
(hopefully not too) stupid questions ;)

My macro:

(define-syntax (my-let-wrong stx)
  (syntax-case stx ()
    ((_ ([x v] ...) e1 e2 ...)
     #`((lambda (x ...) e1 e2 ...) v ...))
    ((_ name ([x v] ...) e1 e2 ...)
     #`(letrec ([name (lambda (x ...) e1 e2 ...)]) (name v ...)))))

When called with:

(define (test-my-let-wrong)
  (my-let-wrong ([fun '(a b c)])
                (my-let-wrong fun ([ls fun])
                              (unless (null? ls)
                                (printf "~s\n" (car ls))
                                (fun (cdr ls))))))

Generates:

((lambda (fun)
     (letrec ([fun (lambda (ls)
                     (unless (null? ls)
                       (printf "~s\n" (car ls))
                       (fun (cdr ls))))])
       (fun fun))) '(a b c))

I believe that I have made the mistake of not taking into
consideration that the function name could shadow the name of an
argument to that function in a given scope.

I believe that this is programmer error, not hygiene violation.


Posted on the users mailing list.