📄 00000004.htm
字号:
previously typed forms. LISP always saves its most recent three <BR>results; it stores them as the values of the symbols *, **, and ***. <BR>For example: <BR> <BR>><I> 3 </I><BR>3 <BR>><I> 4 </I><BR>4 <BR>><I> 5 </I><BR>5 <BR>><I> *** </I><BR>3 <BR>><I> *** </I><BR>4 <BR>><I> *** </I><BR>5 <BR>><I> ** </I><BR>4 <BR>><I> * </I><BR>4 <BR> <BR> <BR> <BR>Special forms <BR> <BR>There are a number of special forms which look like function calls but <BR>aren't. These include control constructs such as if statements and do <BR>loops; assignments like setq, setf, push, and pop; definitions such as <BR>defun and defstruct; and binding constructs such as let. (Not all of <BR>these special forms have been mentioned yet. See below.) <BR> <BR>One useful special form is the quote form: quote prevents its argument <BR>from being evaluated. For example: <BR> <BR>><I> (setq a 3) </I><BR>3 <BR>><I> a </I><BR>3 <BR>><I> (quote a) </I><BR>A <BR>><I> 'a ;'a is an abbreviation for (quote a) </I><BR>A <BR> <BR>Another similar special form is the function form: function causes its <BR>argument to be interpreted as a function rather than being evaluated. <BR>For example: <BR> <BR>><I> (setq + 3) </I><BR>3 <BR>><I> + </I><BR>3 <BR>><I> '+ </I><BR>+ <BR>><I> (function +) </I><BR>#<Function + @ #x-fbef9de> <BR>><I> #'+ ;#'+ is an abbreviation for (function +) </I><BR>#<Function + @ #x-fbef9de> <BR> <BR>The function special form is useful when you want to pass a function as <BR>an argument to another function. See below for some examples of <BR>functions which take functions as arguments. <BR> <BR> <BR> <BR>Binding <BR> <BR>Binding is lexically scoped assignment. It happens to the variables in <BR>a function's parameter list whenever the function is called: the formal <BR>parameters are bound to the actual parameters for the duration of the <BR>function call. You can bind variables anywhere in a program with the <BR>let special form, which looks like this: <BR> <BR> (let ((var1 val1) <BR> (var2 val2) <BR> ... <BR> ) <BR> body <BR> ) <BR> <BR>Let binds var1 to val1, var2 to val2, and so forth; then it executes <BR>the statements in its body. The body of a let follows exactly the same <BR>rules that a function body does. Some examples: <BR> <BR>><I> (let ((a 3)) (+ a 1)) </I><BR>4 <BR>><I> (let ((a 2) </I><BR> (b 3) <BR> (c 0)) <BR> (setq c (+ a b)) <BR> c <BR> ) <BR>5 <BR>><I> (setq c 4) </I><BR>4 <BR>><I> (let ((c 5)) c) </I><BR>5 <BR>><I> c </I><BR>4 <BR> <BR>Instead of (let ((a nil) (b nil)) ...), you can write (let (a b) ...). <BR> <BR>The val1, val2, etc. inside a let cannot reference the variables var1, <BR>var2, etc. that the let is binding. For example, <BR> <BR>><I> (let ((x 1) </I><BR> (y (+ x 1))) <BR> y <BR> ) <BR>Error: Attempt to take the value of the unbound symbol X <BR> <BR>If the symbol x already has a global value, stranger happenings will <BR>result: <BR> <BR>><I> (setq x 7) </I><BR>7 <BR>><I> (let ((x 1) </I><BR> (y (+ x 1))) <BR> y <BR> ) <BR>8 <BR> <BR>The let* special form is just like let except that it allows values to <BR>reference variables defined earlier in the let*. For example, <BR> <BR>><I> (setq x 7) </I><BR>7 <BR>><I> (let* ((x 1) </I><BR> (y (+ x 1))) <BR> y <BR> ) <BR>2 <BR> <BR>The form <BR> <BR> (let* ((x a) <BR> (y b)) <BR> ... <BR> ) <BR> <BR>is equivalent to <BR> <BR> (let ((x a)) <BR> (let ((y b)) <BR> ... <BR> ) ) <BR> <BR> <BR> <BR>Dynamic Scoping <BR> <BR>The let and let* forms provide lexical scoping, which is what you <BR>expect if you're used to programming in C or Pascal. Dynamic scoping is <BR>what you get in BASIC: if you assign a value to a dynamically scoped <BR>variable, every mention of that variable returns that value until you <BR>assign another value to the same variable. <BR> <BR>In LISP, dynamically scoped variables are called special variables. You <BR>can declare a special variable with the defvar special form. Here are <BR>some examples of lexically and dynamically scoped variables. <BR> <BR>In this example, the function check-regular references a regular (ie, <BR>lexically scoped) variable. Since check-regular is lexically outside of <BR>the let which binds regular, check-regular returns the variable's <BR>global value. <BR> <BR>><I> (setq regular 5) </I><BR>5 <BR>><I> (defun check-regular () regular) </I><BR>CHECK-REGULAR <BR>><I> (check-regular) </I><BR>5 <BR>><I> (let ((regular 6)) (check-regular)) </I><BR>5 <BR> <BR>In this example, the function check-special references a special (ie, <BR>dynamically scoped) variable. Since the call to check-special is <BR>temporally inside of the let which binds special, check-special returns <BR>the variable's local value. <BR> <BR>><I> (defvar *special* 5) </I><BR>*SPECIAL* <BR>><I> (defun check-special () *special*) </I><BR>CHECK-SPECIAL <BR>><I> (check-special) </I><BR>5 <BR>><I> (let ((*special* 6)) (check-special)) </I><BR>6 <BR> <BR>By convention, the name of a special variable begins and ends with a *. <BR>Special variables are chiefly used as global variables, since <BR>programmers usually expect lexical scoping for local variables and <BR>dynamic scoping for global variables. <BR> <BR>For more information on the difference between lexical and dynamic <BR>scoping, see _Common LISP: the Language_. <BR> <BR> <BR> <BR>Arrays <BR> <BR>The function make-array makes an array. The aref function accesses its <BR>elements. All elements of an array are initially set to nil. For <BR>example: <BR> <BR>><I> (make-array '(3 3)) </I><BR>#2a((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL)) <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -