[plt-scheme] Re (modified): macros that expand into LISTS OF top-level definitions with computed names

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Aug 14 21:45:14 EDT 2009

I don't know how to define the types first and then create the  
definitions w/o some mapping and whatever through syntax. BUT, when I  
have to do such things I actually define the list of types AND the  
'instructions' at the same time: see below. (My one released instance  
that I can think of is the 'instruction set' for the Universe  
language, see collects/2htdp/universe.ss).

-- Matthias 

#lang scheme

(define register-instruction void)
(define pusher void)

(define-syntax (define-types stx)
  (syntax-case stx ()
    [(_ name type ...)
     (let ([.exec (lambda (t)
                    (define t:str (symbol->string (syntax-e t)))
                    (define t.exec (string-append t:str  ".exec"))
                    (datum->syntax stx (string->symbol t.exec)))])
       (with-syntax ([(index ...) (map .exec (syntax->list (syntax  
(type ...))))])
         #`(begin (define name '(type ...))
                  (define-values (index ...) (values (pusher  
'type) ...))
                  (register-instruction 'type) ...)))]))

;; --- let's "test" ---

(define-types types* exec float integer)

"we define the list of all types"

"... and see these names are defined now"

Posted on the users mailing list.