[racket] syntax-id-rules

From: David Vanderson (david.vanderson at gmail.com)
Date: Thu Dec 8 16:03:40 EST 2011

On 12/08/2011 09:48 AM, Harry Spier wrote:
> I get the error for (get array-element v1[1 1 1]  "expand: unbound
> identifier in module in: get"
I can't speak authoritatively, but I don't think that an identifier 
macro captures any syntax before the identifier.  I think set! is a 
special case.
> and for (set! array-element v1[1 1 1] I get the error of: "set!: bad
> syntax (has 4 parts after keyword) in: (set! array-element v1 (1 1 0)
> 999999)"
>
> Using syntax-id-rules is there a way to write my array-element macro so that :
> (get array-element  v1[1 1 1])
> (set! array-element v1[1 1 0] 999999)
> work correctly without the "get" and "set!" identifiers giving me errors.
>
>
The best I could come up with is:

(define-syntax array-element
   (syntax-id-rules (set!)
     [(set! _ (v[d1] exp)) (vector-set! v d1 exp)]
     [(set! _ (v[d1 d2 ...] exp)) (set! array-element ((vector-ref v 
d1)[d2 ...]exp))]
     [(_ v[d1]) (vector-ref v d1)]
     [(_ v[d1  d2 ...] ) (array-element (vector-ref v d1)[d2 ...] )]))

(array-element  v1[1 1 1])
(set! array-element (v1[1 1 0] 999999))

This removes the "get", and works around the set! problem with extra 
parentheses (although it looks weird to me).  Note that the "set!" 
pattern/templates have to come first.

Hope this helps,
Dave


Posted on the users mailing list.