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

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Sun Feb 3 23:53:04 EST 2008

Oh, I see. Sorry for not getting it in the first reply. It *is* a bug in 
your macro and has nothing to do with hygen. Remember that function is 
first-class value in Scheme, so it is not hard to fix your 
"my-let-wrong" so that the v ... is not in side the scope of "name"

(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 ...))))

Chongkai



Grant Rettke wrote:
> 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.