[plt-scheme] how to modify module variable in macro?

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sat Aug 12 11:31:21 EDT 2006

Im trying to write a macro that adds 1 to a variable each time the macro
is executed, where both macro and variable are defined inside a module.
Ive been reading the docs on syntax but just dont get it yet. Here is
the closest I can manage:

(module m mzscheme

 (provide thing)
 (define-syntax thing
    (let ((foo 0))
      (lambda (stx)
    (syntax-case stx ()
      ((_ body ...)
           (set! foo (add1 foo))
           (printf "foo = ~a\n" foo)
           body ...)))))))

(require m)
(thing (printf "Hello\n"))

--> compile: identifier used out of context in: foo

I know something special has to be done to use module variables inside a
macro but what is that special thing?

Posted on the users mailing list.