123456789101112131415161718192021222324252627282930313233343536373839 |
- ;; I double dog dare you to say this program name outloud.
- ;;
- ;; C's version of 4
- ;;
- ;; for (i = 0; i < 5; i++) {
- ;; printf ("Number is %d\n", i);
- ;; }
- ;;
- ;; Let's add a scheme for loop!
- ;; (for (i 0) (< i 5) (+ i 2)
- ;; (format #t "Number is ~a" i))
- ;; (for (i 0) (< i 5) (+ i 1)
- ;; (format #t "~a\n" i))
- (define-syntax-rule (for (i j-0) (comparison m k-5) (operator n l-1)
- body ...)
- (begin
- (define j-var j-0)
- (define k-var k-5)
- (define l-var l-1)
- (define (generator i)
- (define (loop k-var l-var)
- (cond ((comparison i k-5)
- (set! i (operator i l-1))
- body ...
- (loop k-5 l-1))
- (else '())))
- loop)
- (define run-for (generator j-var))
- (run-for k-5 l-1)))
- (for (i 0) (< i 5) (+ i 1)
- (display "i is: ")
- (format #t "~a\n" i))
- ;; Part 2: can you write this macro without using closures?
|