3 ms·
That isn't lack of lexical scope. It's that the for() syntax isn't purely functional in how it binds variables. One could define an equivalent syntax in Scheme
by brlewis 16y ago
That isn't lack of lexical scope. It's that the for() syntax isn't purely functional in how it binds variables. One could define an equivalent syntax in Scheme and see the same result.
EDIT: http://pastebin.com/7BUXKVXT http://pastebin.com/7BUXKVXT
Paste it into Racket and choose language R5RS.
(define-syntax for
(syntax-rules ()
((for ((var init) condition step) expr ...)
(let ((var init))
(let loop ()
(if condition
(begin
(begin expr ...)
step
(loop))))))))
(define (test-for)
(let ((v (make-vector 10)))
(for ((i 0) (< i 10) (set! i (+ 1 i)))
(vector-set! v i (lambda () i)))
(for ((i 0) (< i 10) (set! i (+ 1 i)))
(display ((vector-ref v i)))
(newline))))