[racket] Another Macro question

From: Harry Spier (harryspier at hotmail.com)
Date: Wed Aug 10 00:17:34 EDT 2011

Firstly many thanks for the explanation of the use of "literal-id" in macros.

As a simple exercise in learning how to use Racket macros, I'm trying to simulate using C style array indices syntax in accessing nested Racket vectors.

I'm able to set up a macro to access a nexted  Racket vector using a syntax like:  (array-ref v3[0 1 2] )

(define v #( #(1 2 3) #(11 22 33) #(111 222 333)))
(define v1 #( #(4 5 6) #(44 55 66) #(444 555 666)))
(define v3 (vector v v1))

(define-syntax array-ref
  (syntax-rules ()
    [(array-ref v [d1]) (vector-ref v d1)]
    [(array-ref v [d1  d2  ... ]) (array-ref (vector-ref v d1) [d2 ... ])]))

I can set up a macro to access a two-dimensional vector with a syntax like (array-ref1 v[0,1])
(define-syntax array-ref1
  (syntax-rules ()
    [(array-ref1 v [d1]) (vector-ref v d1)]
    [(array-ref1 v [d1 , d2 ]) (array-ref1 (array-ref1 v [d1]) [ d2 ])]))

But when  I do the following 
(define-syntax array-ref1
  (syntax-rules ()
    [(array-ref1 v [d1]) (vector-ref v d1)]
    [(array-ref1 v [d1 , d2 ]) (array-ref1 (array-ref1 v [d1]) [ d2 ])]
    [(array-ref v [d1 , d2 , d3]) (array-ref (array-ref (array-ref v [d1]) [d2]) [d3])]))

I get this error:
 syntax-rules: variable used twice in pattern in: unquote

Putting a comma as a literal-id doesn't help, it gives the error 
Module Language: invalid module text
. read: unexpected `)'

Ideally I'd like to be able to set up a macro that can access any level of nested vectors.  
I.e. how do I show in the pattern that I want to repeat not just a pattern variable but ", pattern variable".

Many thanks,
Harry Spier
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110810/a890207f/attachment.html>

Posted on the users mailing list.