Thanks! That was much easier than I was expecting.

For posterity, here is some working code but be warned that I don't know 
whether 'module is the best expansion context to use. But this is a toy 
example anyway.

#lang racket

(define-syntax (collect stx)
  (syntax-case stx (define-values)
    [(_ (define-values (id ...) expr) rest ...)
     #'(let-values ([(id ...) expr])
         (collect rest ...))]
    [(_ (macro arg ...) rest ...)
     (syntax-local-value #'macro (lambda () #f))
     (let ([expanded (local-expand #'(macro arg ...)
                                   'module
                                   (list #'define-values))])
       (syntax-case expanded (define-values)
         [(define-values stuff ...)
          #`(collect #,expanded rest ...)]
         [else
          #'(cons (macro arg ...) (collect rest ...))]))]
    [(_ a rest ...)
     #'(cons a (collect rest ...))]
    [(_)
     #'(list)]))

(define-syntax-rule (my-define stuff ...)
  (define stuff ...))

(define-syntax-rule (my-list stuff ...)
  (list stuff ...))

(collect 1 2 (my-define x 3) x (my-list 4 5))


On Thursday, April 16, 2020 at 9:50:45 PM UTC-5, Matthew Flatt wrote:
>
> The main trick in this case is to recognize `define-values` (which is 
> what `define` expands to) instead of `define`. That's because 
> `define-values` propagates syntax arming to its identifiers and 
> right-hand side, which means that your macro is allowed to pull it 
> apart. 
>
> You'll also need to use an expansion context other than 'expression, 
> though. 
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/8b62651d-86de-4ab8-b9b7-27c4a818dfbf%40googlegroups.com.

Reply via email to