Re: [racket-users] Macro calling macro question

2016-05-20 Thread Kevin Forchione
Both techniques worked for me! Thanks! 

I’m not sure why (format-id #’n “foo~a” [syntax-e #’n)) works when (format-id 
six “foo~a” (syntax-e #’n)) does not though. Apparently I need to look into the 
differences between the two contexts.

-Kevin

> On May 20, 2016, at 2:58 PM, Sam Caldwell  wrote:
> 
> Kevin,
> 
> I have made this exact mistake in the past. The trouble is with the
> lexical context being passed to `format-id`.
> 
> (_foo 3)
> foo3
> ;; 3
> 
> Here, _foo is passed the syntax #'(_foo 3), which came from the same
> environment as the reference, foo3.
> 
> (foo 3)
> foo3
> ;; error ...
> 
> Here, _foo is passed the syntax #'(_foo 3), which was created *by the
> foo macro*, in a different context to the foo3 reference.
> 
> The solution is to pass #'n as the first argument to format-id.
> Hopefully this explanation made some sense.
> 
> - Sam
> 
> On Fri, May 20, 2016 at 5:36 PM, Kevin Forchione  > wrote:
> 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 
> .
> 

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


Re: [racket-users] Macro calling macro question

2016-05-20 Thread Sam Caldwell
Kevin,

I have made this exact mistake in the past. The trouble is with the
lexical context being passed to `format-id`.

(_foo 3)
foo3
;; 3

Here, _foo is passed the syntax #'(_foo 3), which came from the same
environment as the reference, foo3.

(foo 3)
foo3
;; error ...

Here, _foo is passed the syntax #'(_foo 3), which was created *by the
foo macro*, in a different context to the foo3 reference.

The solution is to pass #'n as the first argument to format-id.
Hopefully this explanation made some sense.

- Sam

On Fri, May 20, 2016 at 5:36 PM, Kevin Forchione  wrote:

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

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


Re: [racket-users] Macro calling macro question

2016-05-20 Thread Jens Axel Søgaard
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 :

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


[racket-users] Macro calling macro question

2016-05-20 Thread Kevin Forchione
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.