00000004.htm
来自「一份很好的linux入门资料」· HTM 代码 · 共 1,027 行 · 第 1/5 页
HTM
1,027 行
><I> (aref * 1 1) </I><BR>NIL <BR>><I> (make-array 4) ;1D arrays don't need the extra parens </I><BR>#(NIL NIL NIL NIL) <BR> <BR>Array indices always start at 0. <BR> <BR>See below for how to set the elements of an array. <BR> <BR> <BR> <BR>Strings <BR> <BR>A string is a sequence of characters between double quotes. LISP <BR>represents a string as a variable-length array of characters. You can <BR>write a string which contains a double quote by preceding the quote <BR>with a backslash; a double backslash stands for a single backslash. For <BR>example: <BR> <BR> "abcd" has 4 characters <BR> "\"" has 1 character <BR> "\\" has 1 character <BR> <BR>Here are some functions for dealing with strings: <BR> <BR>><I> (concatenate 'string "abcd" "efg") </I><BR>"abcdefg" <BR>><I> (char "abc" 1) </I><BR>#\b ;LISP writes characters preceded by #\ <BR>><I> (aref "abc" 1) </I><BR>#\b ;remember, strings are really arrays <BR> <BR>The concatenate function can actually work with any type of sequence: <BR> <BR>><I> (concatenate 'string '(#\a #\b) '(#\c)) </I><BR>"abc" <BR>><I> (concatenate 'list "abc" "de") </I><BR>(#\a #\b #\c #\d #\e) <BR>><I> (concatenate 'vector '#(3 3 3) '#(3 3 3)) </I><BR>#(3 3 3 3 3 3) <BR> <BR> <BR> <BR>Structures <BR> <BR>LISP structures are analogous to C structs or Pascal records. Here is <BR>an example: <BR> <BR>><I> (defstruct foo </I><BR> bar <BR> baaz <BR> quux <BR> ) <BR>FOO <BR> <BR>This example defines a data type called foo which is a structure <BR>containing 3 fields. It also defines 4 functions which operate on this <BR>data type: make-foo, foo-bar, foo-baaz, and foo-quux. The first one <BR>makes a new object of type foo; the others access the fields of an <BR>object of type foo. Here is how to use these functions: <BR> <BR>><I> (make-foo) </I><BR>#s(FOO :BAR NIL :BAAZ NIL :QUUX NIL) <BR>><I> (make-foo :baaz 3) </I><BR>#s(FOO :BAR NIL :BAAZ 3 :QUUX NIL) <BR>><I> (foo-bar *) </I><BR>NIL <BR>><I> (foo-baaz **) </I><BR>3 <BR> <BR>The make-foo function can take a keyword argument for each of the <BR>fields a structure of type foo can have. The field access functions <BR>each take one argument, a structure of type foo, and return the <BR>appropriate field. <BR> <BR>See below for how to set the fields of a structure. <BR> <BR> <BR> <BR>Setf <BR> <BR>Certain forms in LISP naturally define a memory location. For example, <BR>if the value of x is a structure of type foo, then (foo-bar x) defines <BR>the bar field of the value of x. Or, if the value of y is a one- <BR>dimensional array, (aref y 2) defines the third element of y. <BR> <BR>The setf special form uses its first argument to define a place in <BR>memory, evaluates its second argument, and stores the resulting value <BR>in the resulting memory location. For example, <BR> <BR>><I> (setq a (make-array 3)) </I><BR>#(NIL NIL NIL) <BR>><I> (aref a 1) </I><BR>NIL <BR>><I> (setf (aref a 1) 3) </I><BR>3 <BR>><I> a </I><BR>#(NIL 3 NIL) <BR>><I> (aref a 1) </I><BR>3 <BR>><I> (defstruct foo bar) </I><BR>FOO <BR>><I> (setq a (make-foo)) </I><BR>#s(FOO :BAR NIL) <BR>><I> (foo-bar a) </I><BR>NIL <BR>><I> (setf (foo-bar a) 3) </I><BR>3 <BR>><I> a </I><BR>#s(FOO :BAR 3) <BR>><I> (foo-bar a) </I><BR>3 <BR> <BR>Setf is the only way to set the fields of a structure or the elements <BR>of an array. <BR> <BR>Here are some more examples of setf and related functions. <BR> <BR>><I> (setf a (make-array 1)) ;setf on a variable is equivalent to setq </I><BR>#(NIL) <BR>><I> (push 5 (aref a 1)) ;push can act like setf </I><BR>(5) <BR>><I> (pop (aref a 1)) ;so can pop </I><BR>5 <BR>><I> (setf (aref a 1) 5) </I><BR>5 <BR>><I> (incf (aref a 1)) ;incf reads from a place, increments, </I><BR>6 ;and writes back <BR>><I> (aref a 1) </I><BR>6 <BR> <BR> <BR> <BR>Booleans and Conditionals <BR> <BR>LISP uses the self-evaluating symbol nil to mean false. Anything other <BR>than nil means true. Unless we have a reason not to, we usually use the <BR>self-evaluating symbol t to stand for true. <BR> <BR>LISP provides a standard set of logical functions, for example and, or, <BR>and not. The and and or connectives are short-circuiting: and will not <BR>evaluate any arguments to the right of the first one which evaluates to <BR>nil, while or will not evaluate any arguments to the right of the first <BR>one which evaluates to t. <BR> <BR>LISP also provides several special forms for conditional execution. The <BR>simplest of these is if. The first argument of if determines whether <BR>the second or third argument will be executed: <BR> <BR>><I> (if t 5 6) </I><BR>5 <BR>><I> (if nil 5 6) </I><BR>6 <BR>><I> (if 4 5 6) </I><BR>5 <BR> <BR>If you need to put more than one statement in the then or else clause <BR>of an if statement, you can use the progn special form. Progn executes <BR>each statement in its body, then returns the value of the final one. <BR> <BR>><I> (setq a 7) </I><BR>7 <BR>><I> (setq b 0) </I><BR>0 <BR>><I> (setq c 5) </I><BR>5 <BR>><I> (if (> a 5) </I><BR> (progn <BR> (setq a (+ b 7)) <BR> (setq b (+ c 8))) <BR> (setq b 4) <BR> ) <BR>13 <BR> <BR>An if statement which lacks either a then or an else clause can be <BR>written using the when or unless special form: <BR> <BR>><I> (when t 3) </I><BR>3 <BR>><I> (when nil 3) </I><BR>NIL <BR>><I> (unless t 3) </I><BR>NIL <BR>><I> (unless nil 3) </I><BR>3 <BR> <BR>When and unless, unlike if, allow any number of statements in their <BR>bodies. (Eg, (when x a b c) is equivalent to (if x (progn a b c)).) <BR> <BR>><I> (when t </I><BR> (setq a 5) <BR> (+ a 6) <BR> ) <BR>11 <BR> <BR>More complicated conditionals can be defined using the cond special <BR>form, which is equivalent to an if ... else if ... fi construction. <BR> <BR>A cond consists of the symbol cond followed by a number of cond <BR>clauses, each of which is a list. The first element of a cond clause is <BR>the condition; the remaining elements (if any) are the action. The cond <BR>form finds the first clause whose condition evaluates to true (ie, <BR>doesn't evaluate to nil); it then executes the corresponding action and <BR>returns the resulting value. None of the remaining conditions are <BR>evaluated; nor are any actions except the one corresponding to the <BR>selected condition. For example: <BR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?