[racket] variables within macros

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Wed Jan 16 10:31:02 EST 2013

Hi Tim,
Racket's macros expand one step at a time. Your module begin will expand in one step before the inner expr ... all get expanded, thus leaving the counter at 0. What you might want to do is local expand the body of your module and count the define-values forms in the module-begin itself. You could also just do a local-expand for the effect on counter to get your result, but I don't recommend this.
-Ian
----- Original Message -----
From: "Tim Brown" <tim.brown at cityc.co.uk>
To: "Racket Users" <users at racket-lang.org>
Sent: Wednesday, January 16, 2013 9:24:44 AM GMT -05:00 US/Canada Eastern
Subject: [racket] variables within macros

Folks,

I would like to write a module that uses a macro to transform "define"s
and accumulate a value based on those transformations.

In a fairly simple way, I want to do something like the below. However, it
seems the module-begin is being transformed before the my-defines. How do
I get counter to be incremented before I need it?

Tim

===================================================================================
"counted-defines.rkt" (similar to what's on the 17.1.2 Using #lang s-exp 
Guide Page)
===================================================================================
#lang racket
(provide (except-out (all-from-out racket) #%module-begin define)
          (rename-out (module-begin #%module-begin) (my-define define)))

(require (for-syntax syntax/parse))
(define-for-syntax counter 0)

(define-syntax (my-define stx)
   (syntax-parse
    stx
    [(_ i v) (set! counter (add1 counter)) #`(define i v)]
    [(_ (i args ...) v ...+) (set! counter (add1 counter))
                             #`(define (i args ...) v ...)]))

(define-syntax (module-begin stx)
   (syntax-parse
    stx
    [(_ expr ...)
     #`(#%module-begin expr ...
        (define defines-count #,counter)
        (provide defines-count))]))


==========================
"test-counted-defines.rkt"
==========================
#lang s-exp "counted-defines.rkt"
(define woo 2) ; should have pushed counter up by one?


==========================
I then run: racket -e '(require "test-counted-defines.rkt") defines-count'
Which returns me "0"



-- 
Tim Brown <tim.brown at cityc.co.uk>  | City Computing Limited            |
T: +44 20 8770 2110                | City House, Sutton Park Road      |
F: +44 20 8770 2130                | Sutton, Surrey, SM1 2AE, GB       |
-----------------------------------------------------------------------|
BEAUTY:  What's in your eye when you have a bee in your hand           |
-----------------------------------------------------------------------'
City Computing Limited registered in London No. 1767817.
Registered Office: City House, Sutton Park Road, Sutton, Surrey, SM1 2AE
VAT number 372 8290 34.
____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Posted on the users mailing list.