[racket] Macros: "define" a variable with a transformed id

From: Christopher Bowron (cwbowron at gmail.com)
Date: Fri Aug 13 17:46:26 EDT 2010

I'm trying to write a macro that takes in an id, and creates a
variable with a name based on id, and adds a string based on id into a

If I have (define-filter-category foo), I want it to add
"CICategoryFoo" to the FILTER-CATEGORIES list as well as introduce a
binding for the variable filter-category-foo.  I have the part that
adds the string to the list, but I do not know how to manipulate the
id and introduce the variable.  Can anyone shed some light on this for
me?  Thanks

What I have so far:

(define FILTER-CATEGORIES empty)

(define (id->category-string n)
  (define (camel-case str)
    (regexp-replace* #rx"-" (string-titlecase str) ""))
  (string-append "CICategory" (camel-case (symbol->string n))))

(define (id->category-id n)
  (string->symbol (string-append "filter-category-" (symbol->string n))))

(define-syntax define-filter-category
  (syntax-rules ()
    ((_ id string)
       ;;; FIXME: also (define ,id  string)
       (set! FILTER-CATEGORIES (cons string FILTER-CATEGORIES))))
    ((_ name)
       (id->category-id (quote name))
       (id->category-string (quote name))))))

(define-filter-category distortion-effect)
(define-filter-category ...)

Christopher W. Bowron <chris at bowron.us>
[ Nothing is exciting if you know what the outcome will be ]
    - Joseph Campbell

Posted on the users mailing list.