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

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sat Aug 12 12:33:30 EDT 2006

Nevermind, I overlooked the obvious answer..

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

And it could be cleaned up, but as long as it works..

Jon Rafkind wrote:
> 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 ...)
>        (syntax
>          (begin
>            (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?
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>
>   


Posted on the users mailing list.