[plt-scheme] how to quote identifier in macro

From: Andre Mayers (andre.mayers at usherbrooke.ca)
Date: Mon Dec 15 08:36:06 EST 2008

You have introduced new scheme constructs that I didn't know before. 
I will be happy to know more about them, can you suggest any references. 
And your solution works well, 
thank you Noel,

Andre


-----Message d'origine-----
De : Noel Welsh [mailto:noelwelsh at gmail.com] 
Envoyé : December-14-08 4:58 PM
À : andre.mayers at usherbrooke.ca
Cc : pltscheme
Objet : Re: [plt-scheme] how to quote identifier in macro

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.