2021年4月11日星期日

Common LISP: Lexical vs Dynamic Binding?

I read a relevant post on binding, however still have questions.

Here are the following examples I found. Can someone tell me if the conclusions are correct?

Dynamic Binding of x in (i):

(defun j ()   (let ((x 1))      (i)))    (defun i ()     (+ x x))    > (j)  2  

Lexical Binding of x in i2:

(defun i2 (x)         (+ x x))    (defun k ()     (let ((x 1))         (i2 2)))    > (k)  4  

No Global Lexical Variables in ANSI CL so Dynamic Binding is performed:

(setq x 3)    (defun z () x)    > (let ((x 4)) (z))  4  

Dynamic Binding, which appears to bind to a lexically scoped variable:

(defvar x 1)  (defun f (x) (g 2))  (defun g (y) (+ x y))  > (f 5)  7  

Based on the above tests, CL first tries lexical binding. If there is no lexical match in the environment, then CL tries dynamic binding. It appears that any previously lexically scoped variables become available to dynamic binding. Is this correct? If not, what is the behavior?

https://stackoverflow.com/questions/67052464/common-lisp-lexical-vs-dynamic-binding April 12, 2021 at 11:54AM

没有评论:

发表评论