[racket] [Tutorial?] How to make a macro that takes a procedure as argument

From: Laurent (laurent.orseau at gmail.com)
Date: Thu Sep 16 14:13:15 EDT 2010

> It's true that macro arguments are syntax objects, and the rest of your
> tutorial shows one way of interpreting a syntax object as an "expression at
> compile time" (as opposed to "expression at run time", which is what most
> macro arguments are).
> It's also possible to do the evaluation immediately, instead of
> trampolining through define-syntax. See syntax-local-eval from
> unstable/syntax. It creates an internal definition context, uses
> syntax-local-bind-syntaxes to evaluate and bind the expression to a
> temporary name, and syntax-local-value to extract the value.

Excellent! This is so much simpler. Thanks!

Now we can write the following (also using to-syntax to simplify a little
bit more):

#lang racket

(require (for-syntax unstable/syntax))

(define-syntax (define-me2 stx)
  (syntax-case stx ()
    [(_ id-maker var val)
     (with-syntax ([id (to-syntax
                        #:stx #'var
                         ((syntax-local-eval #'id-maker)
                          (symbol->string (syntax->datum #'var))
       #'(define id val))]))

(define-me2 (λ(id-str)(string-append id-str "-me2"))
  a 2)


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

Posted on the users mailing list.