[plt-scheme] hygienic macros
I am not sure what Eli is getting at. However, it seems that as written
_rand_ gains little from being a macro in Scheme. It would be better as
a procedure in this instance.
To take advantage of the fact that ideally the macros are expanded at
load-time. It would be really nice for the macro to yield the _syntax_
of the random choice rather than _code_ that picks the random choice.
Something like this:
(define-syntax rand-pick
(lambda (stx)
(syntax-case stx ()
((_ exp1 exprs ...)
(let ((stx-list (syntax-e (syntax (exp1 exprs ...)))))
(list-ref stx-list (random (length stx-list))))))))
I am sure I missed the issues that Eli was referring to..
-Bruce
michael rice wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> 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))
> exprs))))
>
>
> 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
> ls)))))))
> (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
> http://promotions.yahoo.com/design_giveaway/
>
>