I'm wondering if...

https://stackoverflow.com/questions/41102630/transforming-a-list-of-symbols-to-a-list-of-identifiers-to-be-used-in-a-macro

is what I'm looking for (he says, answering himself)...

(define-syntax (introduce stx)
  (syntax-case stx ()
    [(_ tag ids ...)
     (with-syntax ([(getter ...) (datum->syntax #'tag
                                                (map (λ (id)
                                                       (string->symbol
(format "~a-get-~a"

   (syntax->datum #'tag)

   id)))
                                                     (syntax->datum #'(ids
...))))])
       #`(begin
           (define (getter)
             ;; Generating a do-nothing body for demonstration...
             (format "get ~a ~a" (quote tag) (quote getter))) ...
                                                              ))]))

Here's one answer. And I just crossed with Jens...

Many thanks, Jens. That's much nicer. It illustrates how to decompose the
problem into a few clean helpers to make the resulting macro much clearer.

Thank you,
Matt

On Tue, Dec 11, 2018 at 5:07 PM Matt Jadud <m...@jadud.com> wrote:

> Many thanks, John.
>
> I've made it that far. I'll be more specific...
>
> Conceptually, I want...
>
> (define-syntax (introduce stx)
>   (syntax-case stx ()
>     [(_ tag ids ...)
>      (with-syntax ([(getters ...)
>                     (map (λ (id) (format-id #'tag "~a-get-~a" tag id)) ids
> ...)])
>        #`(begin
>            (define (getters) 'get-something) ...)
>        )]))
>
> so that
>
> (introduce tag a b c)
>
> produces
>
> (define (tag-get-a) ...)
> (define (tag-get-b) ...)
> (define (tag-get-c) ...)
>
> I am aware that I can't use 'map' in the context above. I... know, but do
> not understand, that the LHS of with-syntax is a syntax pattern. However,
> how I would generate a syntactic form that I would match against that
> pattern where I generate the identifiers for the defines (and then how I
> would appropriately use ... to indicate repetition of the defines form, if
> the structure above is even close) is what I'm wrestling with.
>
> Cheers,
> M
>
>
>
> On Tue, Dec 11, 2018 at 4:16 PM John Clements <cleme...@brinckerhoff.org>
> wrote:
>
>> In answer to at least one of your questions, top-level “begin”s are
>> “spliced” into their context to produce top-level bindings. So, for
>> instance,
>>
>> #lang racket
>>
>> (begin
>>   (define a 3)
>>   (define b 4))
>>
>> (+ b a)
>>
>> … evaluates to 7
>>
>> I think this is not the only issue you’re going to run into, but it’s at
>> least one of them.
>>
>> Apologies if I misunderstood your question!
>>
>> John
>>
>> > On Dec 11, 2018, at 1:12 PM, Matt Jadud <m...@jadud.com> wrote:
>> >
>> > Hi all,
>> >
>> > I'm exploring macros today.
>> >
>> > I'd like to introduce a set of bindings. For example, I would like
>> >
>> > (introduce tag a b c)
>> >
>> > to become
>> >
>> > (define (tag-get-a) ...)
>> > (define (tag-set-a! v) ...)
>> >
>> > or somesuch set of defines. I've discovered with-syntax and format-id,
>> and have had good success with introducing singular defines, but how to
>> expand the list of ids into a list of defines is something I'm not seeing
>> yet. Somehow, I feel like a macro that simply follows the natural recursion
>> on the syntax objects is probably the answer, but... that's like saying the
>> answer to a question in quantum mechanics is either 0, 1, or h-bar/2...
>> >
>> > I'm looking at the define-cbr example in the documentation, and
>> suspicious that I'm reaching a point with my macro authoring where the ...
>> and (... ...) are leaving me confused as to which layer of expansion I'm
>> dealing with as I look at my code. Hence, I'm asking for a nudge in the
>> right direction.
>> >
>> > Cheers,
>> > Matt
>> >
>> >
>> >
>> > --
>> > 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.

Reply via email to