The following code is confusing me:
(define (even-or-odd-letrec x)
((lambda (internal-even? internal-odd?)
(set! internal-even? (lambda (n)
(if (= n 0) 'even
(internal-odd? (- n 1)))))
(set! internal-odd? (lambda (n)
(if (= n 0) 'odd
(internal-even? (- n 1)))))
(internal-even? x))
#f #f))
As I read it, the environments are as follows:
- Inside the environment of
even-or-odd-letrec,internal-even?andinternal-odd?are initially bound to#f. Their parent environment is the global environment. set!then changes these two values to the obviouslambdas, but does not change the environments.- Because the environments haven't changed, any calls to
internal-even?will look in the global environment forinternal-odd?and find nothing.
So how does this code work?