[plt-scheme] Macro question

From: Benderjg2 at aol.com (Benderjg2 at aol.com)
Date: Sat May 24 16:43:36 EDT 2003

In a message dated 5/23/2003 10:13:07 PM Central Daylight Time, 
mcj4 at ukc.ac.uk writes:

> I get a complaint about #%app not being bound, in particular with respect 
> to my calls to 
> 'vector-ref'. I've read the docs on this a few times; I don't think it's 
> unclear, but I do think I 
> don't understand.

This error message should be really read as "macro messed with the hygiene;
the module system detects that the author got the hygiene marks wrong" ;)

I actually get a different error message when I use your code and example:
> (let ((v (vector 1 2 3))
>         (v2 (vector 4 5))
>         (v3 (vector 6)))
>     (bind ((v a b c) (v2 d e) (v3 f))
>           (list v.a v.b v.c v2.d v2.e v3.f)))
gives me an error that v is not bound, which is really the same problem
(that is, the syntax "marks" on v in the macro result are somehow not 
the same as those for the v bound in my own let binding).

I think part of the problem is that you throw away all syntax info very
early on, with the call to syntax-object->datum. Then, in building up the
let bindings, it is unclear actually what syntax information will be 
associated
with both the vector binding names (v, v2, and v3) and vector-ref. To be 
safer
only the v.a, v.b, etc. should be "washed" through datum->syntax-object.

The following code works, producing "(1 2 3 4 5 6)" for the above example.
Note that I produce the list of let bindings as a list of syntax, rather than 
lists.

#cs
(module bind mzscheme
  
  (provide bind)
  
  (define-syntax (bind e)
    (define (process-binding vnm flst)
      (let loop ((pos 0)
                 (flst flst))
        (if (null? flst)
            '()
            (cons 
             #`(#,(datum->syntax-object 
                   e
                   (string->symbol 
                    (string-append (symbol->string (syntax-object->datum 
vnm))
                                   "."
                                   (symbol->string (syntax-object->datum (car 
flst))))))
                 (vector-ref #,vnm #,pos))
             (loop (+ pos 1) (cdr flst))))))
    (define (make-bindings v-names flst-lists)
      (apply append
             (map process-binding v-names flst-lists)))
    (syntax-case e ()
      ((_ ((v* f ...) ...) code ...)
       #`(let #,(make-bindings (syntax->list #'(v* ...))
                               (map syntax->list (syntax->list #'((f ...) 
...))))
           code ...)))))

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20030524/596f0806/attachment.html>

Posted on the users mailing list.