[racket] Another Macro question

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Wed Aug 10 02:30:36 EDT 2011

On 2011-08-10 00:17:34 -0400, Harry Spier wrote:
>    I get this error:
>     syntax-rules: variable used twice in pattern in: unquote

The problem here is that "," is a special character used by the Reader
for unquoting. You can't put it in a macro keyword list because the
reader tries to read ",)" as unquoting ")".

You *can* put "unquote" in the keyword list though.

(define-syntax array-ref
  (syntax-rules (unquote)
    [(_ v [d1]) (vector-ref v d1)]
    [(_ v [d1 , d2 ])
     (array-ref (array-ref v [d1]) [ d2 ])] 
    [(_ v [d1 , d2 , d3])
     (array-ref (array-ref (array-ref v [d1]) [d2]) [d3])]))

However, this macro is a bit dubious because it's abusing unquote. It'd
be better to avoid using "," as a keyword in your macro since it's
confusing to people who are used to reading Racket code.

Cheers,
Asumu


Posted on the users mailing list.