[plt-scheme] Introducing bindings in a macro

From: Doug Williams (m.douglas.williams at gmail.com)
Date: Thu Jan 17 10:33:07 EST 2008

I'd like to introduce bindings in a macro.  The commented code for
with-my-struct is more what I would like to see, but it doesn't work,  The
uncommented code works, but is more complex and, well, uglier.  Is there a
clearer way (i.e. closer to the commented code) to write it?

(module dummy mzscheme

  (provide (all-defined))

  (define-struct my-struct (a b c))

;  (define-syntax with-my-struct
;    (syntax-rules ()
;      ((with-my-struct s
;        expr ...)
;       (let ((a (my-struct-a s))
;             (b (my-struct-b s))
;             (c (my-struct-c s)))
;         expr ...))))

  (define-syntax (with-my-struct stx)
    (syntax-case stx ()
      ((with-my-struct s
         expr ...)
       (with-syntax ((a (datum->syntax-object (syntax with-my-struct) 'a))
                     (b (datum->syntax-object (syntax with-my-struct) 'b))
                     (c (datum->syntax-object (syntax with-my-struct) 'c)))
         (syntax
          (let ((a (my-struct-a s))
                (b (my-struct-b s))
                (c (my-struct-c s)))
            expr ...))))))


  (define (test-with-my-struct)
    (let ((s (make-my-struct 1 2 3)))
      (with-my-struct s
        (printf "a = ~a, b = ~a, c = ~a~n" a b c))))

  (test-with-my-struct)

  )

Thanks,
Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080117/a982c097/attachment.html>

Posted on the users mailing list.