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

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Tue Nov 4 13:29:19 EST 2008

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)
              (string-append "nv-" (symbol->string (syntax->datum #'name)))))])
        (syntax/loc stx
          (define new-name val)))])))

(defsymbol biz 4)


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.