Help with simple macro transformation

27 views
Skip to first unread message

Ryan Kramer

unread,
Feb 18, 2020, 12:44:59 AM2/18/20
to Racket Users
I'm 95% sure I've done this before, but for some reason I am really stuck right now. Given

(rearrange ([(a b c) 1]
           
[(d e) 2]))

I would like any kind of shape containing [a 1] [b 1] [c 1] [d 2] [e 2]. Any kind of nesting should be fine as long as the ids are matched up 1:1 with the numbers. I've tried variants of the following but always run in to "incompatible ellipsis" problems:

(define-syntax-rule (rearrange [(id ...) body] ...)
 
'(([id body] ...)
    ...))

Can this be done with patterns, or do I need to use quasisyntax splicing and list manipulation?

Larger context: I am making a table of proc-ids and proc-bodies. Some procs share the same bodies, so I want to be able to write [(a b c) shared-body], but eventually I need to get to [a shared-body] ...

Ryan Kramer

unread,
Feb 18, 2020, 1:23:55 AM2/18/20
to Racket Users
Gah, of course it's possible with patterns. For some reason I just didn't think of matching `(id rest ...)`

(define-syntax (rearrange stx)
 
(syntax-case stx ()
   
[(_ [(id rest ...) body]
        more
...)
     
#'(cons '[id body]
             
(rearrange [(rest ...) body]
                        more
...))]
   
[(_ [() body]
        more
...)
     
#'(rearrange more ...)]
   
[(_)
     
#'(list)]))


Reply all
Reply to author
Forward
0 new messages