📄 00000004.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>From: <A HREF="mailto:apson.bbs@bbs.ntu.edu.tw">apson.bbs@bbs.ntu.edu.tw</A> (apson) <BR>Newsgroups: ntu.dep.im <BR>Subject: LISP使用简介 <BR> <BR> <BR> Common LISP Hints <BR> Geoffrey J. Gordon <BR> <<A HREF="mailto:ggordon@cs.cmu.edu>">ggordon@cs.cmu.edu></A> <BR> Friday, February 5, 1993 <BR> <BR> Modified by <BR> Bruno Haible <BR> <<A HREF="mailto:haible@ma2s2.mathematik.uni-karlsruhe.de>">haible@ma2s2.mathematik.uni-karlsruhe.de></A> <BR> <BR>Note: This tutorial introduction to Common Lisp was written for the <BR>CMU environment, so some of the details of running lisp toward the end <BR>may differ from site to site. <BR> <BR> <BR>Further Information <BR> <BR>The best LISP textbook I know of is <BR> <BR> Guy L. Steele Jr. _Common LISP: the Language_. Digital Press. 1984. <BR> <BR>The first edition is easier to read; the second describes a more recent <BR>standard. (The differences between the two standards shouldn't affect <BR>casual programmers.) <BR> <BR>A book by Dave Touretsky has also been recommended to me, although I <BR>haven't read it, so I can't say anything about it. <BR> <BR> <BR> <BR>Symbols <BR> <BR>A symbol is just a string of characters. There are restrictions on what <BR>you can include in a symbol and what the first character can be, but as <BR>long as you stick to letters, digits, and hyphens, you'll be safe. <BR>(Except that if you use only digits and possibly an initial hyphen, <BR>LISP will think you typed an integer rather than a symbol.) Some <BR>examples of symbols: <BR> <BR> a <BR> b <BR> c1 <BR> foo <BR> bar <BR> baaz-quux-garply <BR> <BR>Some things you can do with symbols follow. (Things after a ">" prompt <BR>are what you type to the LISP interpreter, while other things are what <BR>the LISP interpreter prints back to you. The ";" is LISP's comment <BR>character: everything from a ";" to the end of line is ignored.) <BR> <BR>><I> (setq a 5) ;store a number as the value of a symbol </I><BR>5 <BR>><I> a ;take the value of a symbol </I><BR>5 <BR>><I> (let ((a 6)) a) ;bind the value of a symbol temporarily to 6 </I><BR>6 <BR>><I> a ;the value returns to 5 once the let is finished </I><BR>5 <BR>><I> (+ a 6) ;use the value of a symbol as an argument to a functio. </I><BR>11 <BR>><I> b ;try to take the value of a symbol which has no value </I><BR>Error: Attempt to take the value of the unbound symbol B <BR> <BR>There are two special symbols, t and nil. The value of t is defined <BR>always to be t, and the value of nil is defined always to be nil. LISP <BR>uses t and nil to represent true and false. An example of this use is <BR>in the if statement, described more fully later: <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>The last example is odd but correct: nil means false, and anything else <BR>means true. (Unless we have a reason to do otherwise, we use t to mean <BR>true, just for the sake of clarity.) <BR> <BR>Symbols like t and nil are called self-evaluating symbols, because <BR>they evaluate to themselves. There is a whole class of self-evaluating <BR>symbols called keywords; any symbol whose name starts with a colon is a <BR>keyword. (See below for some uses for keywords.) Some examples: <BR> <BR>><I> :this-is-a-keyword </I><BR>∶<I>THIS-IS-A-KEYWORD </I><BR>><I> :so-is-this </I><BR>∶<I>SO-IS-THIS </I><BR>><I> :me-too </I><BR>∶<I>ME-TOO </I><BR> <BR> <BR> <BR>Numbers <BR> <BR>An integer is a string of digits optionally preceded by + or -. A real <BR>number looks like an integer, except that it has a decimal point and <BR>optionally can be written in scientific notation. A rational looks like <BR>two integers with a / between them. LISP supports complex numbers, <BR>which are written #c(r i) (where r is the real part and i is the <BR>imaginary part). A number is any of the above. Here are some numbers: <BR> <BR> 5 <BR> 17 <BR> -34 <BR> +6 <BR> 3.1415 <BR> 1.722e-15 <BR> #c(1.722e-15 0.75) <BR> <BR>The standard arithmetic functions are all available: +, -, *, /, floor, <BR>ceiling, mod, sin, cos, tan, sqrt, exp, expt, and so forth. All of them <BR>accept any kind of number as an argument. +, -, *, and / return a <BR>number according to type contagion: an integer plus a rational is a <BR>rational, a rational plus a real is a real, and a real plus a complex <BR>is a complex. Here are some examples: <BR> <BR>><I> (+ 3 3/4) ;type contagion </I><BR>15/4 <BR>><I> (exp 1) ;e </I><BR>2.7182817 <BR>><I> (exp 3) ;e*e*e </I><BR>20.085537 <BR>><I> (expt 3 4.2) ;exponent with a base other than e </I><BR>100.90418 <BR>><I> (+ 5 6 7 (* 8 9 10)) ;the fns +-*/ all accept multiple arguments </I><BR> <BR>There is no limit to the absolute value of an integer except the memory <BR>size of your computer. Be warned that computations with bignums (as <BR>large integers are called) can be slow. (So can computations with <BR>rationals, especially compared to the corresponding computations with <BR>small integers or floats.) <BR> <BR> <BR> <BR>Conses <BR> <BR>A cons is just a two-field record. The fields are called "car" and <BR>"cdr", for historical reasons. (On the first machine where LISP was <BR>implemented, there were two instructions CAR and CDR which stood for <BR>"contents of address register" and "contents of decrement register". <BR>Conses were implemented using these two registers.) <BR> <BR>Conses are easy to use: <BR> <BR>><I> (cons 4 5) ;Allocate a cons. Set the car to 4 and the cdr to 5. </I><BR>(4 . 5) <BR>><I> (cons (cons 4 5) 6) </I><BR>((4 . 5) . 6) <BR>><I> (car (cons 4 5)) </I><BR>4 <BR>><I> (cdr (cons 4 5)) </I><BR>5 <BR> <BR> <BR> <BR>Lists <BR> <BR>You can build many structures out of conses. Perhaps the simplest is a <BR>linked list: the car of each cons points to one of the elements of the <BR>list, and the cdr points either to another cons or to nil. You can <BR>create such a linked list with the list fuction: <BR> <BR>><I> (list 4 5 6) </I><BR>(4 5 6) <BR> <BR>Notice that LISP prints linked lists a special way: it omits some of <BR>the periods and parentheses. The rule is: if the cdr of a cons is nil, <BR>LISP doesn't bother to print the period or the nil; and if the cdr of <BR>cons A is cons B, then LISP doesn't bother to print the period for cons <BR>A or the parentheses for cons B. So: <BR> <BR>><I> (cons 4 nil) </I><BR>(4) <BR>><I> (cons 4 (cons 5 6)) </I><BR>(4 5 . 6) <BR>><I> (cons 4 (cons 5 (cons 6 nil))) </I><BR>(4 5 6) <BR> <BR>The last example is exactly equivalent to the call (list 4 5 6). Note <BR>that nil now means the list with no elements: the cdr of (a b), a list <BR>with 2 elements, is (b), a list with 1 element; and the cdr of (b), a <BR>list with 1 element, is nil, which therefore must be a list with no <BR>elements. <BR> <BR>The car and cdr of nil are defined to be nil. <BR> <BR>If you store your list in a variable, you can make it act like a stack: <BR> <BR>><I> (setq a nil) </I><BR>NIL <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -