(letrec # (letrec bindings body ...) -> object # where bindings have the form ((variable1 init1)...) # This means there are 4 bindings (counter, reset, n, current). ( (counter # 1. binding: variable1=counter () '()) # init1='() an empty list. (reset # 2. binding: variable1=reset (lambda # init1=lambda method which is defined as "(lambda formals body ...) -> procedure". () # lambda formals: empty list. (set! # lambda body: set method defined as "(set! variable expression) -> object" sets an empty list to counter. counter '()))) # This means reset is bound a method which resets the variable counter. (n # 3. binding: variable1=n (lambda # init1=lambda method (index) # lambda formals: variable index. (do # lambda body: do method defined as "(do bindings terminator body ...) -> object or nil" where bindings has the form ((variable1 init1 step1)...), terminator (test expression ...), and body command ... . ( # do bindings: 2x (i 0 (+ i 1)) # 1: variable1=i, init1=0, step1=+ i 1 ( c # 2: variable2=c '() # init2='() (append c # step2= append something to c if ... (if # (= i index) # ... i=index ... (if (>= i (length counter)) # (list 1) # ... and if i>=length of counter then append list (1) to c. (list (+ (list-ref counter i) 1)) # ... and if iindex ... (>= i (length counter)) # ... and if i>= length of counter then (list 0) # ... append a list (0) to c. (list (list-ref counter i)) # ... and if i i index) # do terminator (set! counter c) # do command? ) ) ) ) (current (lambda () counter) ) ) # end of letrec bindings (add-method (auto-num (number-type) num) (if (= num 0) (begin (reset) 0) (n (- num 1)) ) ) (add-method (auto-num-string (number-type) num sep) (let ( (r "") (psep "") ) (map (lambda (e) (set! r (append r psep (number->string e)) ) (set! psep sep) ) counter ) (append r " ") ) ) ) ############### # New Version # ############### (find-operation auto-num) (add-method (auto-num (number-type) num) (letrec ((counter '()) (reset (lambda () (set! counter '()))) (n (lambda (index) (do ((i 0 (+ i 1)) (c '() (append c (if (= i index) (if (>= i (length counter)) (list 1) (list (+ (list-ref counter i) 1))) (if (>= i (length counter)) (list 0) (list (list-ref counter i))))))) ((> i index) (set! counter c))))) (current (lambda () counter))) (if (= num 0) (begin (reset) 0) (n (- num 1)) )) )