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.
SBCL 2.2.9.debian gives 20 for the 1st example, as expected. OP might want to double-check that result.