[plt-scheme] how to quote identifier in macro

From: Noel Welsh (noelwelsh at gmail.com)
Date: Sun Dec 14 16:57:50 EST 2008

You must introduce bindings at expansion time, but you cannot compute
their value till eval time.  Your soln was trying to compute values at
expansion time.  See below.

#lang scheme

;; Import cut which we use below
(require (for-syntax srfi/26/cut))

;; Give attr a transformer binding.  This means we can lookup the
value of attr given its name at expansion time, using
syntax-local-value
(define-syntax attr '(a1 a2 a3))

;; : stx symbol -> stx
;; Given a symbol construct an identifier bound in the context of context
(define-for-syntax (make-id context id)
  (datum->syntax context id context))

;; : alist symbol -> any
(define (slot-value self attr-id)
  (cadr (assq attr-id self)))

(define-syntax (tata stx)
 (syntax-case stx ()
   [(_ a (s ps ...) body ...)
    (with-syntax ([(attr-id ...)
                   ;; Given the name of the attributes lookup their
value (at expansion time)
                   ;; Then convert the attribute names to identifiers
in the context we're expanding
                   (map (cut make-id stx <>)
                        (syntax-local-value (syntax a)))])
      (syntax
       (λ (s ps ...)
         (let ([attr-id (slot-value s (quote attr-id))] ...)
           body ...))))]))

((tata attr
     (self p1 p2)
     (display self) (newline) (+ p2 a1))
 '((a1 10)(a2 20)(a3 30)) 4 5)


HTH,
N.

Posted on the users mailing list.