[racket] Question on (quote var) in macros

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Wed Jan 16 00:27:28 EST 2013

Untested but I think this will work

(define-syntax (my-macro stx)
  (syntax-case stx ()
    [(_ (id1 id2 ...) body ...)
     #'(lambda (expr)
           (match expr [(list 'id1 id2 ...) body ...]))]))

On 01/15/2013 10:22 PM, Paul Meier wrote:
> Hi friends,
>
> I'm working on a macro that expands into a match statement. It looks something like
>
> (define-syntax (my-macro stx)
>   (syntax-case stx ()
>     [(_ (id1 id2 ...) body ...)
>      (with-syntax ([match-pattern 
>                           (datum->syntax stx (cons 'list (syntax->datum #'(id1 id2 ...))))])
>      #'(lambda (expr)
>          (match expr
>             [match-pattern body ...])))))
>
> So in effect, it expands
>
> (my-macro (push i) (printf "~v" i)
>
> into
>
> (lambda (expr)
>   (match expr
>     [(list push i) (printf "~v" i)]))
>
> Here is the problem: I'd like the first entry of the match list to be quoted (e.g. 'push) rather than unquoted, as it is above, since 'match' will interpret this as a free variable, meaning
>
> ((my-macro (push i) i) '(dont-push 45)) will evaluate to 45, rather than fail to match (the desired behavior).
>
> Unfortunately, trusty (quote (syntax->datum stx id1)) and or any variation thereof will do what it's supposed to: quote whatever expression will evaluate into whatever I want quoted.
>
> I could just write the quotes into the expressions using the macro, a la (my-macro ('push i) ...), but Shirly there's a way around this?
>
> Any help would be appreciated. Thanks for your time; I'm loving my explorations into Racket ^_^
>
> -Paul
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130115/c8f46990/attachment.html>

Posted on the users mailing list.