|
|
 |
 |
 |
 |
Scheme Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Capturing the load-time value of a procedure being wrapped
(define foo (lambda () 9)) (define foo (let ((foo foo)) (lambda () (+ 1 (foo))))) What should (foo) return? MIT/GNU-Scheme, SCM, GSI, Mzscheme, and Scheme48 return 10. Is this behavior guaranteed by R5RS?
On Mar 26, 11:07 am, Aubrey Jaffer <a@alum.mit.edu> wrote: > (define foo (lambda () 9)) > (define foo (let ((foo foo)) (lambda () (+ 1 (foo))))) > What should (foo) return? > MIT/GNU-Scheme, SCM, GSI, Mzscheme, and Scheme48 return 10. > Is this behavior guaranteed by R5RS?
This should be guaranteed by the static lexical scoping rules for "let". When bindings are created by "let", a new environment is created, the bindings are evaluated one by one, and after all evaluation is complete, all the bindings are added to the new environment. This means that the "foo" created by the "let" is bound to a lambda expression of zero args, returning the constant integer 9. It is only AFTER this that the lambda expression is bound to the identifier "foo" in the top level environment by the second "define". When (foo) is applied, the lambda is executed "in the environment within which it was defined", which has "foo" bound to a lambda of zero args returning the constant 9. This definition of "foo" effectively "shadows" the definition of foo created by the second define. Hope this helps. Brian C. Barnes
Aubrey Jaffer wrote: > (define foo (lambda () 9)) > (define foo (let ((foo foo)) (lambda () (+ 1 (foo))))) > What should (foo) return? > MIT/GNU-Scheme, SCM, GSI, Mzscheme, and Scheme48 return 10. > Is this behavior guaranteed by R5RS?
I think it's guaranteed by R5RS section 5.2.1 [1]. Was this a trick question? Will [1] http://www-swiss.ai.mit.edu/~jaffer/r5rs_7.html#SEC45
|
 |
 |
 |
 |
|