The macro application (foo 3) expands into

                   #'(begin
                        (_foo n0)
                        (foo n ...))

The expansion of (_foo n0) then defines foo3, but
the scope will only include the piece of code above.

To make definitions made by sub-macros available
at the original macro application site, you can use
syntax-local-introduce:

(define-syntax (foo stx)
  (syntax-case stx ()
    [(_)          #'(begin)]
    [(_ n0 n ...) (syntax-local-introduce
                    #'(begin
                        (_foo n0)
                        (foo n ...)))]))

Note that I changed the pattern variable to _ in order not to
show shadow the binding of foo.

This might not be strictly necessary here, but if you do
run into such a case, it will be hard to spot the error.

/Jens Axel










2016-05-20 23:36 GMT+02:00 Kevin Forchione <lyss...@gmail.com>:

> Hi guys,
> I’ve been interested in having a macro build a series of defines. So I
> decided to start small, trying to get to a macro that would do something
> like the following to begin with:
>
> >(foo 3)
> (define foo1 1)
> (define foo2 2)
> (define foo3 3)
>
> I start with a macro that appears to do a single define:
>
> (require (for-syntax racket/syntax))
>
> (define-syntax (_foo stx)
>   (syntax-case stx ()
>     [(_ n) (with-syntax ([id (format-id stx "foo~a" (syntax-e #'n))])
>              #'(define id n))]))
>
>
> And this appears to work for (_foo 3) for instance.
>
> Then I create a macro that calls this macro:
>
> (define-syntax (foo stx)
>   (syntax-case stx ()
>     [(foo n0) #'(_foo n0)]
>     [(foo n0 n ...) #'(begin
>                       (_foo n0)
>                       (foo n ...))]))
>
> thinking that I could do something like (foo 1 2 3) as an intermediary
> step.  So I test it with (foo 3) for instance, expecting it to define foo3
> and the macro debugger tells me that it’s created (define foo3 3), but that
> foo3 is bound as foo3.0, which is a mystery to me as I thought building the
> id using with-syntax and format-id  would have sorted the binding for this.
>
> Looks like I’m at a learning moment…. any explanation why executing (_foo
> 3) at the real works and (foo 3) does not?
>
> Thanks!
> -Kevin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
-- 
Jens Axel Søgaard

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to