Common Lisp scoping (dynamic vs lexical)

天大地大妈咪最大 提交于 2019-12-02 19:37:45

The effects of setting an undefined variable using SETF is undefined in ANSI Common Lisp.

DEFVAR will define a special variable. This declaration is global and also has effect on LET bindings. That's the reason that by convention these variables are written as *foo*. If you have ever defined X with DEFVAR, it is declared special and there is no way to declare it lexical later.

LET by default provides local lexical variables. If the variable was already declared special (for example because of a DEFVAR), then it just creates a new local dynamic binding.

Update

  • Example 1 .

Nothing to see.

  • Example 2

Xhas been declared special. All uses of the variable X now use dynamic binding. When calling the function, you bind X to 5. Dynamically. Other functions can now access this dynamic binding and get that value.

  • Example 3

This is undefined behavior in Common Lisp. You are setting an undeclared variable. What happens then is implementation dependent. Your implementation (most do something similar) sets the symbol value of X to 100. In FUN1, X is lexically bound. In FUN2 evaluating X retrieves the symbol value (or possibly to the dynamically bound value) of X.

As an example for an implementation that did (does?) something else: the CMUCL implementation would also have declare X in example 3 by default to be special. Setting an undefined variable also declares it special.

NOTE

In portable standard compliant Common Lisp code the global variables are defined with DEFVAR and DEFPARAMETER. Both declare these variables to be special. ALL uses of these variables now involve dynamic binding.

Remember:

((lambda (x)
   (sin x))
 10)

is basically the same as

(let ((x 10))
  (sin x))

Which means that variable bindings in LET bindings and variable bindings in function calls are working the same way. If X would have been declared special in some place earlier, both would involve dynamic binding.

This is specified in the Common Lisp standard. See for example the explanation to the SPECIAL declaration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!