[plt-scheme] Macro question

From: David Van Horn (dvanhorn at cs.uvm.edu)
Date: Sat May 24 00:55:48 EDT 2003

mcj4 at ukc.ac.uk wrote:
> Hello,
> 
> I'm working on my macro-fu. In my imagination, the form
> 
> (bind ([v c1 c2 c3] [v2 c4 c5] [v3 c6]) 'thisisatest)
> 
> expands to  =>
>  
>  (let ((v.c1 (vector-ref v 0)) 
>        (v.c2 (vector-ref v 1)) 
>        (v.c3 (vector-ref v 2))
>        (v2.c4 (vector-ref v2 0))
>        (v2.c5 (vector-ref v2 1))
>        (v3.c6 (vector-ref v3 0))) 
>    (quote thisisatest))

This gets you pretty close.  There's no v.c style binding, but it's compact
and clear.

BTW, I couldn't reproduce your #%app not bound error...

(define-syntax bind
  (syntax-rules ()
    ((_ ([v c1 c2 ...] ...) e ...)
     (let-values ([(c1 c2 ...) (vector->values v)]
                  ...)
       e ...))))

(define (vector->values v)
  (apply values (vector->list v)))

(define v1 #(1 2 3))
(define v2 #(4 5))

(bind ([v1 c1 c2 c3]
       [v2 c4 c5])
  (+ c1 c2 c3 c4 c5))  ;; -> 15

-d




Posted on the users mailing list.