[plt-scheme] How to let a new symbol (created in _syntax-case_) visible at top level ?

From: Andre Mayers (andre.mayers at usherbrooke.ca)
Date: Tue Nov 4 15:09:50 EST 2008

Thank you, 

I didn't understand the use of the first (context) and the third (source location) parameters of datum->syntax. Now, it is clear. 

André



-----Message d'origine-----
De : carl.eastlund at gmail.com [mailto:carl.eastlund at gmail.com] De la part de Carl Eastlund
Envoyé : November-04-08 1:29 PM
À : andre.mayers at usherbrooke.ca
Cc : PLT Scheme Mailing List
Objet : Re: [plt-scheme] How to let a new symbol (created in _syntax-case_) visible at top level ?

Here's a version that ought to work.  It constructs new-name with the
lexical context of the input syntax, so it will be visible in the
macro user's scope.  It constructs the define, however, using the
context of the macro definition, so that even if the macro user is in
some funny language with a different binding (or no binding) for
"define", this macro will still behave properly.

#lang scheme
(define-syntax defsymbol
 (λ (stx)
   (syntax-case stx ()
     [(_ name val)
      (with-syntax
          ([new-name
            (datum->syntax
             #'name
             (string->symbol
              (string-append "nv-" (symbol->string (syntax->datum #'name)))))])
        (syntax/loc stx
          (define new-name val)))])))

(defsymbol biz 4)

nv-biz

On Tue, Nov 4, 2008 at 12:46 PM, Andre Mayers
<andre.mayers at usherbrooke.ca> wrote:
> #lang scheme
> (define-syntax defsymbol
>  (λ (stx)
>    (syntax-case stx ()
>      [(_ name val)
>       (with-syntax
>           ([new-name
>             (string->symbol
>              (string-append "nv-" (symbol->string (syntax->datum #'name))))])
>         (datum->syntax
>          #f
>          (list #'define (syntax->datum #'new-name) #'val)
>          #f))])))
>
> (defsymbol biz 4)
>
> nv-biz
> ==>
> expand: unbound identifier in module in: nv-biz
>
> I am pretty sure that I am not the first to ask the question but I can't find the answer.
>
>
> 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/~amayers



Posted on the users mailing list.