Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Design Patterns

Name: Anonymous 2011-04-21 16:07

Fuck em!

Name: Anonymous 2011-04-22 15:55

>>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

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List