[racket-users] Macro generating macro question

19 views
Skip to first unread message

Kevin Forchione

unread,
May 24, 2023, 12:27:08 AM5/24/23
to Racket-Users List
Hi guys,
I’m stumped. In a nutshell I want to write a macro that is passed an id and will produce a macro called id that can take variable arguments. I’m sure I’m overlooking something fundamental. The basic form below “works” if I don’t have ellipsis aver the variables, but that’s not what I’m after. Here’s an example that is obviously wrong, but is along the lines of what I’m looking for:

#lang racket


(require (for-syntax syntax/parse
racket/syntax))

(define-syntax (make-id-macro stx)
(syntax-parse stx
[(_ id)
(with-syntax ([name (format-id #'id "do-~a" #'id)])
#'(define-syntax (name stx)
(syntax-parse stx
[(_ parms ...)
#'( list parms ...)])))]))

Any help in this and explaining why it fails would be greatly appreciated.

-Kevin

Stephen De Gabrielle

unread,
May 24, 2023, 3:45:57 AM5/24/23
to Kevin Forchione, Racket-Users List
Hi Kevin 

The most active places for ‘Racketeers’ is the Racket [Discourse](https://racket.discourse.group/) and [Discord](https://discord.gg/6Zq8sH5


Please join us!


See also https://github.com/racket/racket/wiki/Macros - in particular i think you may find what you are looking for in 


(many who prefer email just use discourse in ‘mailing list mode’)


Best regards

Stephen


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/beaa2ef6-afd2-4686-829a-390eb69f5620n%40googlegroups.com.

Beyond the Racket Users Google Group, Racket Discussions take place on Discourse ( https://racket.discourse.group/ ) and Discord ( https://discord.gg/6Zq8sH5 ). Discussion (but less active) also takes place on the Racket Slack https://racket.slack.com/ ( sign up at https://racket-slack.herokuapp.com/ ), and IRC #racket https://kiwiirc.com/nextclient/irc.libera.chat/#racket
---
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/6BC84910-3AC4-4729-8BAA-D1488E84A54B%40gmail.com.
--
----

Jens Axel Søgaard

unread,
May 24, 2023, 5:12:00 AM5/24/23
to Kevin Forchione, Racket-Users List
Hi Kevin,

When you are writing macro generating macros there are two levels of ellipsis.
The outer one, which you can write as ... as normal and the inner one which you need to escape. 
You can escape it with (... ...). That looks odd to me, so I usually bind it to an identifier named ooo.

#lang racket
(require (for-syntax syntax/parse
                     racket/syntax))

(define-syntax (make-id-macro stx)
  (syntax-parse stx
    [(_ id)
     (with-syntax ([name (format-id #'id "do-~a" #'id)]
                   [ooo  #'(... ...)])

       #'(define-syntax (name stx)
           (syntax-parse stx
             [(_ parms ooo)
              #'(list parms ooo)])))]))

(make-id-macro foo)
(do-foo 1 2)


See you on Racket Discourse.
/Jens Axel


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/beaa2ef6-afd2-4686-829a-390eb69f5620n%40googlegroups.com.

Beyond the Racket Users Google Group, Racket Discussions take place on Discourse ( https://racket.discourse.group/ ) and Discord ( https://discord.gg/6Zq8sH5 ). Discussion (but less active) also takes place on the Racket Slack https://racket.slack.com/ ( sign up at https://racket-slack.herokuapp.com/ ), and IRC #racket https://kiwiirc.com/nextclient/irc.libera.chat/#racket
---
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/6BC84910-3AC4-4729-8BAA-D1488E84A54B%40gmail.com.


--
--
Jens Axel Søgaard

Kevin Forchione

unread,
May 24, 2023, 2:59:57 PM5/24/23
to Jens Axel Søgaard, Racket-Users List


On May 24, 2023, at 2:11 AM, Jens Axel Søgaard <jens...@soegaard.net> wrote:

#lang racket
(require (for-syntax syntax/parse
                     racket/syntax))

(define-syntax (make-id-macro stx)
  (syntax-parse stx
    [(_ id)
     (with-syntax ([name (format-id #'id "do-~a" #'id)]
                   [ooo  #'(... ...)])
       #'(define-syntax (name stx)
           (syntax-parse stx
             [(_ parms ooo)
              #'(list parms ooo)])))]))

(make-id-macro foo)
(do-foo 1 2)

Thanks! That was the fundamental concept I was missing. I don’t recall seeing that in the documentation, but now that I know I’ll search for that and see if I glossed over it somehow. 

-Kevin
Reply all
Reply to author
Forward
0 new messages