[plt-scheme] Struggling with macro...

From: Robert Nikander (nikander at nc.rr.com)
Date: Wed May 23 11:15:18 EDT 2007

Paulo J. Matos wrote:
> It would probably be easier to dump with-syntax and just define a
> separate macro for this but I can't for a very simple reason (to which
> I don't know a workaround), I need to access the mc variable from the
> macro, so the macro needs to be declared after mc is defined and in
> the same environment, due to hygiene.
> 
> Any ideas?

I think you can call helper functions without having to deal with a new 
hygienic "layer" or whatever it's called.   For example, the different 
'mc' identifiers here appear to work as desired:

(define (add-variable-to-mc m n v)
   (printf "added var: ~a ~a ~a~n" m n v))
(define (add-thingy-to-mc m t)
   (printf "added thingy: ~a ~a~n" m t))
(define (make-mc . args)
   'the-mc)

(define-syntax (machine stx)
    (syntax-case stx ()
      [(_ mname sexps ...)
       (identifier? #'mname) ; fender
       (let* ([sections (syntax->list #'(sexps ...))]
              [adds (map (λ (section)
                           (syntax-case section (variables thingies)
                             ((variables (name value) ...)
                              #'(begin (add-variable-to-mc mc 'name 
value) ...))
                             ((thingies expr ...)
                              #'(begin (add-thingy-to-mc mc expr) ...))))
                         sections)])
         #`(let ([mc (make-mc () () () () ())])
              #, at adds
               mc))]))

(printf "Testing~n")
(machine hello
          (variables (x 1)
                     (y 2)
                     (z 3))
          (thingies 10 20))




Posted on the users mailing list.