#lang play ;; loops (36.3.1 del PLAI) (defmac (for0 from to in ...) #:keywords from to in (local ((define loop (lambda () (cond ((<= ) (begin ... (loop (+ 1)))))))) (loop ))) (for0 x from 2 to 5 in (display x)) (newline) ;; not right because evaluates high several times ;(for0 x from 2 to (read) in (display x)) ;; eval upper once (defmac (for1 from to in ...) #:keywords from to in (local ((define high-val ) (define loop (lambda () (cond ((<= high-val) (begin ... (loop (+ 1)))))))) (loop ))) ;; still not right because evaluates high before low ;(for1 x from (read) to (read) in (display x)) ;; Implicit iteration identifiers (36.3.2 del PLAI) (defmac (for2 from to in ...) #:keywords from to in (local ((define high-val ) (define loop (lambda (it) (cond ((<= it high-val) (begin ... (loop (+ it 1)))))))) (loop ))) ;(for2 from 2 to 5 in (display it)) ;; hygiene -- now we want to introduce a capture (36.3.3 del PLAI) (defmac (for3 from to in ...) #:keywords from to in #:captures it (local ((define high-val ) (define loop (lambda (it) (cond ((<= it high-val) (begin ... (loop (+ it 1)))))))) (loop ))) (for3 from 2 to 9 in (display it)) (newline) ;; nested loops (lost outer it in inner loop) (for3 from 2 to 5 in (for3 from 1 to it in (display it)) (newline))