[plt-scheme] hygienic macros

From: michael rice (nowgate at yahoo.com)
Date: Sat Apr 3 13:42:55 EST 2004

To firm up my understanding of hygienic macros I have
been trying to translate a few from the old backquote
syntax to the new define-syntax, case-syntax, etc.

The latest one I chose is a random-choice macro from
Paul Graham's ANSI Common Lisp (pg. 170):

(defmacro random-choice (&rest exprs)
`(case (random ,(length exprs))
   , at let ((key -1))
     (mapcar #'(lambda (expr)
                 `(,(incf key) ,expr))

My translation(shorter pick replaces random-choice):

;randomly pick an expression to evaluate
(define-syntax pick
  (lambda (x)
    (syntax-case x ()
      ((_ exprs ...)
       (with-syntax (((index ...)
                      (let f ((i 0) (ls (syntax-e
(syntax (exprs ...)))))
                        (if (null? ls)
                            (cons i (f (+ i 1) (cdr
         (syntax (pick-help (index ...) (exprs

(define-syntax pick-help
  (lambda (x)
    (syntax-case x ()
      ((_ (index ...) (exprs ...))
       (with-syntax (((clauses ...)
                      (map (lambda (i e) (list (list
i) e))
                           (syntax-e (syntax (index
                           (syntax-e (syntax (exprs
         (syntax (case (random (length '(clauses
...))) clauses ...)))))))

Eli Barzilay gave me the hint that allowed me to
complete it and suggested that "There are certain
issues in your code that might worth a discussion..."
Pick it apart if you will (pun intended).

Michael Rice


Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway 

Posted on the users mailing list.