>>34
That's so easy and stupid
!
(define-for-syntax count 0)
(define-syntax (counter stx)
(syntax-case stx ()
((~ x)
(exact-nonnegative-integer? (syntax->datum #'x))
#`#,(begin0 count (set! count (syntax->datum #'x))))
(~ (identifier? #'~) #`#,(begin0 count (set! count (add1 count))))))
counter counter counter counter counter counter ; 0 1 2 3 4 5
(counter 3) counter counter ; 6 3 4
The next is ``now make a macro that defines a counter macro'', right
!
(define-syntax-rule (define-counter id)
(begin
(define-for-syntax count 0)
(define-syntax (id stx)
(syntax-case stx ()
((~ x)
(exact-nonnegative-integer? (syntax->datum #'x))
#`#,(begin0 count (set! count (syntax->datum #'x))))
(~ (identifier? #'~) #`#,(begin0 count (set! count (add1 count))))))))
(define-counter a)
(define-counter b)
a a a a ; 0 1 2 3
b b b ; 0 1 2
a b a b ; 4 3 5 4