[plt-scheme] Hygienic Macros
Paulo,
Here's another one (probably Eli's examples are better, but I've been
bitten by this one in practice):
Suppose you define a macro do-range:
(define-syntax do-range
(syntax-rules ()
((do-range (i llow hhigh) body ...)
(let ((low llow)
(high hhigh))
(let loop ((i low))
(if (< i high)
(begin body ...
(loop (add1 i)))
'done))))))
And then try to use it in
(define (vector-n-smallest vector n <)
(let* ((store (vector-copy vector 0 (+ n 1))))
(insert-sort! store <)
(do-range (i (+ n 1) (vector-length vector))
(vector-set! store n (vector-ref vector i))
(insert-sort! store <))
(vector-copy store 0 n)))
Without hygiene, the '< in the do-range macro will refer to the '<
that is passed into vector-n-smallest. There's no way to avoid this
without introducing hygiene (i.e. it's not a bug that can be fixed by
simply using (gensym)). And, it bit me in real life :).
Will
On Thu, Feb 12, 2009 at 7:07 AM, Paulo J. Matos <pocmatos at gmail.com> wrote:
> Hi all,
>
> What would be a good simple example of an hygienic macro that would
> not work as expected if the macro were not hygienic?
> I would like an example that would be simple to understand to
> programming language students that are non-schemers. All the ones I
> have found from papers don't seem 'simple' enough.
>
> Any suggestions?
>
> --
> Paulo Jorge Matos - pocmatos at gmail.com
> Webpage: http://www.personal.soton.ac.uk/pocm
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>