I’m learning common lisp and the subtleties of scoping really troubles me. For example, running this piece of (“double-let”) code
(let ((x 10))
(funcall
(let((x 20))
(lambda () x ))))
gives me 20 in Clisp and 10 in SBCL. I’m not even sure this is a dynamic/lexical scoping issue, because a typical example that demonstrates scoping would concern special variables defined with defvar/defparameter, like
(defvar *x* 10)
(defun foo ()
(let ((*x* 20))
(bar)))
(defun bar ()
(print *x*))
(foo)
a dynamic-binding language is supposed to give 10, and lexical-binding one 20. (BTW I tested this with Clisp and SBCL, both gave me 10.)
So what is causing different behavior with the “double-let” example? Does SBCL not create an closure with the lambda function?
I would appreciate it if someone would explain, or point out references.
Regarding your second example explanation: you are mixing up dynamic extend and lexical scope.
If only lexical scope would be available for this second source code, then
bar
could not see the value of 20 for*x*
and always would print10
. (Try this example inC/C++
, which only uses lexical scope, see below). But since*x*
is a special variable and rules of dynamic extend apply,bar
sees the value of 20, when called byfoo
and therefore prints 20.SBCL returned for both examples the value of 20 on my system, btw.
C-Example:
#include int x = 10; void bar(); void foo() { int x = 20; bar(); } void bar(){ printf("%i\n",x); } void main() { foo(); } // this program prints 10, as expected