Re: [racket-users] Expanding a macro into multiple syntax objects (Defining two things with one macro)
Bizarre, I was for some reason very convinced that begin defined its own scope! I guess I usually called it in the positions in which it becomes an expression as opposed to being used in its special form with splicing. The macro is working like a charm now. Thanks, I thought the solution would be a lot rougher than that! On Thursday, June 22, 2017 at 3:53:27 PM UTC-4, Sam Caldwell wrote: > Are you sure that `begin` doesn't suffice for your needs? It has a somewhat > unintuitive scoping behavior. From the docs [1] > > The begin form is special at the top level, at module level, or as a body > after only internal definitions. In those positions, instead of forming an > expression, the content of begin is spliced into the surrounding context. > > Example: > > > (let ([curly 0]) > (begin > (define moe (+ 1 curly)) > (define larry (+ 1 moe))) > (list larry curly moe)) > > '(2 0 1) > > - Sam Caldwell > > [1] http://docs.racket-lang.org/guide/begin.html > > > On Thu, Jun 22, 2017 at 3:40 PM, Sam Waxmanwrote: > Hello, > > > > It's simple enough to write a macro that defines something. > > > > (define-syntax-rule (my-define name binding) > > (define name binding)) > > > > But what if I would like to define multiple things? I.e. > > > > (define-syntax-rule (my-multiple-define name ... binding ...) > > (define name binding) ...) > > > > The above is no good, because define-syntax-rule expects only one body to be > returned, not multiple. Wrapping the defines in begin wouldn't work, because > then they'd only be able to be accessed in the scope of that begin (when, in > actuality, I want the rest of the code to access them, like they would be > able to in the first example). > > > > Similarly, if I "upgrade" the syntax-rule to define-syntax, we run into the > same problems. I thought that the following would work, > > > > (define-syntax (my-multiple-define stx) > > (syntax-case stx () > > [(_ name1 name2 binding1 binding2) > > #'(define name1 binding1) > > #'(define name2 binding2)])) > > > > but it looks like this only returns the last syntax object, not both of them. > > > > (Note, my actual goal here is to define something, then define a syntax rule > afterwards like > > > > (define-syntax (my-multiple-define stx) > > (syntax-case stx () > > [(_ x y z) > > #'(define x y) > > #'(define-syntax-rule (z *stuff*) > > *some random body to the syntax-rule*)])) > > > > , so the solution of using define-values to do all the defines in one step > won't work for me.) > > > > I'd be satisfied either with knowing how to make a macro expand into multiple > syntax objects (so that one macro can expand into both defines), or with > someone letting me know how to define something, then define a syntax rule > afterwards using only one syntax object (like wrapping them in a begin but > that bumps the definitions inside to the outer scope). > > > > Many thanks in advance! > > > > -- > > 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...@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] Expanding a macro into multiple syntax objects (Defining two things with one macro)
Are you sure that `begin` doesn't suffice for your needs? It has a somewhat unintuitive scoping behavior. From the docs [1] The begin form is special at the top level, at module level, or as a body after only internal definitions. In those positions, instead of forming an expression, the content of begin is spliced into the surrounding context. Example: > (let ([curly 0]) (begin (define moe (+ 1 curly)) (define larry (+ 1 moe))) (list larry curly moe)) '(2 0 1) - Sam Caldwell [1] http://docs.racket-lang.org/guide/begin.html On Thu, Jun 22, 2017 at 3:40 PM, Sam Waxmanwrote: > Hello, > > It's simple enough to write a macro that defines something. > > (define-syntax-rule (my-define name binding) >(define name binding)) > > But what if I would like to define multiple things? I.e. > > (define-syntax-rule (my-multiple-define name ... binding ...) >(define name binding) ...) > > The above is no good, because define-syntax-rule expects only one body to > be returned, not multiple. Wrapping the defines in begin wouldn't work, > because then they'd only be able to be accessed in the scope of that begin > (when, in actuality, I want the rest of the code to access them, like they > would be able to in the first example). > > Similarly, if I "upgrade" the syntax-rule to define-syntax, we run into > the same problems. I thought that the following would work, > > (define-syntax (my-multiple-define stx) >(syntax-case stx () > [(_ name1 name2 binding1 binding2) >#'(define name1 binding1) >#'(define name2 binding2)])) > > but it looks like this only returns the last syntax object, not both of > them. > > (Note, my actual goal here is to define something, then define a syntax > rule afterwards like > > (define-syntax (my-multiple-define stx) >(syntax-case stx () > [(_ x y z) >#'(define x y) >#'(define-syntax-rule (z *stuff*) >*some random body to the syntax-rule*)])) > > , so the solution of using define-values to do all the defines in one step > won't work for me.) > > I'd be satisfied either with knowing how to make a macro expand into > multiple syntax objects (so that one macro can expand into both defines), > or with someone letting me know how to define something, then define a > syntax rule afterwards using only one syntax object (like wrapping them in > a begin but that bumps the definitions inside to the outer scope). > > Many thanks in advance! > > -- > 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] Expanding a macro into multiple syntax objects (Defining two things with one macro)
Hi Sam, I'm a little unclear on your intended use case since (begin ...) does splice defines, for instance: (define (add-something x) (begin (define one 1) (define two 2)) (+ x one two)) ;; Welcome to DrRacket, version 6.9 [3m]. Language: racket/base, with debugging; memory limit: 2048 MB. > (add-something 5) 8 Can you be more clear on what's going wrong? Deren On Thu, Jun 22, 2017 at 3:40 PM, Sam Waxmanwrote: > Hello, > > It's simple enough to write a macro that defines something. > > (define-syntax-rule (my-define name binding) >(define name binding)) > > But what if I would like to define multiple things? I.e. > > (define-syntax-rule (my-multiple-define name ... binding ...) >(define name binding) ...) > > The above is no good, because define-syntax-rule expects only one body to > be returned, not multiple. Wrapping the defines in begin wouldn't work, > because then they'd only be able to be accessed in the scope of that begin > (when, in actuality, I want the rest of the code to access them, like they > would be able to in the first example). > > Similarly, if I "upgrade" the syntax-rule to define-syntax, we run into > the same problems. I thought that the following would work, > > (define-syntax (my-multiple-define stx) >(syntax-case stx () > [(_ name1 name2 binding1 binding2) >#'(define name1 binding1) >#'(define name2 binding2)])) > > but it looks like this only returns the last syntax object, not both of > them. > > (Note, my actual goal here is to define something, then define a syntax > rule afterwards like > > (define-syntax (my-multiple-define stx) >(syntax-case stx () > [(_ x y z) >#'(define x y) >#'(define-syntax-rule (z *stuff*) >*some random body to the syntax-rule*)])) > > , so the solution of using define-values to do all the defines in one step > won't work for me.) > > I'd be satisfied either with knowing how to make a macro expand into > multiple syntax objects (so that one macro can expand into both defines), > or with someone letting me know how to define something, then define a > syntax rule afterwards using only one syntax object (like wrapping them in > a begin but that bumps the definitions inside to the outer scope). > > Many thanks in advance! > > -- > 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.
[racket-users] Expanding a macro into multiple syntax objects (Defining two things with one macro)
Hello, It's simple enough to write a macro that defines something. (define-syntax-rule (my-define name binding) (define name binding)) But what if I would like to define multiple things? I.e. (define-syntax-rule (my-multiple-define name ... binding ...) (define name binding) ...) The above is no good, because define-syntax-rule expects only one body to be returned, not multiple. Wrapping the defines in begin wouldn't work, because then they'd only be able to be accessed in the scope of that begin (when, in actuality, I want the rest of the code to access them, like they would be able to in the first example). Similarly, if I "upgrade" the syntax-rule to define-syntax, we run into the same problems. I thought that the following would work, (define-syntax (my-multiple-define stx) (syntax-case stx () [(_ name1 name2 binding1 binding2) #'(define name1 binding1) #'(define name2 binding2)])) but it looks like this only returns the last syntax object, not both of them. (Note, my actual goal here is to define something, then define a syntax rule afterwards like (define-syntax (my-multiple-define stx) (syntax-case stx () [(_ x y z) #'(define x y) #'(define-syntax-rule (z *stuff*) *some random body to the syntax-rule*)])) , so the solution of using define-values to do all the defines in one step won't work for me.) I'd be satisfied either with knowing how to make a macro expand into multiple syntax objects (so that one macro can expand into both defines), or with someone letting me know how to define something, then define a syntax rule afterwards using only one syntax object (like wrapping them in a begin but that bumps the definitions inside to the outer scope). Many thanks in advance! -- 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.