《Common LISP Hints 中译》
《Common LISP Hints》
Geoffrey J. Gordon
Modified by Bruno Haible
Transtalted to Traditional Chinese by Wei-Yueh Chen (陈玮岳)
QUOTE:
Note: This tutorial introduction to Common Lisp was written for the CMU environment, so some of the details of running lisp toward the end may differ from site to site.
注:本 Common Lisp 教学文件是针对 CMU 版本的 Lisp ,所以使用者之间可能会因为采用的 Lisp 版本不同,在执行细节上有些微差异。
QUOTE:
Further Information
The best LISP textbook I know of is Guy L. Steele Jr. _Common LISP: the Language_. Digital Press. 1984.
The first edition is easier to read; the second describes a more recent standard. (The differences between the two standards shouldn't affect casual programmers.)
A book by Dave Touretsky has also been recommended to me, although I haven't read it, so I can't say anything about it.
我所知到最好的 Lisp 教科书是 Guy L. Steele Jr. 所写的 Common LISP: the Language ,该书是在 1984. 由 Digital Press 出版社所出版,它的第一版很容易读,第二版则描述了更多最新的标准。(对于一般的程序设计师而言,第一、二版关于最新标准的些微差异并不会有虾咪影响。)
另外还有一本由 Dave Touretsky 所写的书也有很多人跟我推荐,不过由于我并没有去读过,所以我也无法评论。
QUOTE: Symbols
A symbol is just a string of characters. There are restrictions on what
you can include in a symbol and what the first character can be, but as long as you stick to letters, digits, and hyphens, you'll be safe. (Except that if you use only digits and possibly an initial hyphen, LISP will think you typed an integer rather than a symbol.) Some examples of symbols: a b c1 foo bar
baaz-quux-garply
Symbols(字符)
symbol 就是一串字符,不过这串字符里面可以使用的字符跟起始的第一个字符是稍有限制的,
但是原则上只要你使用〝大小写字母、数字、减号(连接符号)〞来混合产生 symbol,应该就不会有问题。
(如果你只有使用使用数字的,并且至多在拿减号当启始字符,那将会被 Lisp 视为是数字,而不是 symbol 。)
下面是 symbol 的一些范例: a b c1 foo bar
baaz-quux-garply
QUOTE:
Some things you can do with symbols follow. (Things after a \LISP interpreter, while other things are what the LISP interpreter prints back to you. The \LISP's comment character: everything from a \> (setq a 5) ;store a number as the value of a symbol 5
> a ;take the value of a symbol 5
> (let ((a 6)) a) ;bind the value of a symbol temporarily to 6 6
> a ;the value returns to 5 once the let is finished 5
> (+ a 6) ;use the value of a symbol as an argument to a function 11
> b ;try to take the value of a symbol which has no value Error: Attempt to take the value of the unbound symbol B
你可以像下面的例子一样的使用 symbol 。
(在 > 提示符号后面的就是你的输入给 Lisp 直译器的内容,而其它的就是 Lisp 直译器所送回来输出的结果。而 \分号则是 Lisp 的批注符号,在分号之后到该行结束的数据都会被直译器所忽略。)
> (setq a 5) ; 把数值 5 存入 a 这个 symbol 里面。 5
> a ; 取得 a 这个 symbol 所存的值。 5
> (let ((a 6)) a) ; 暂时性地把 a 这个 symbol 的值给设定成 6 6
> a ; 当脱离 let 区块之后, a 的值又变回到 5 5
> (+ a 6) ; 把 a 这个 symbol 的值当作是加法函数的参数 11
> b ; 尝试着取得并没有值的 b 这个 symbol 的值看会发生虾咪事情? Error: Attempt to take the value of the unbound symbol B
QUOTE:
There are two special symbols, t and nil. The value of t is defined always to be t, and the value of nil is defined always to be nil. LISP uses t and nil to represent true and false. An example of this use is in the if statement, described more fully later:
> (if t 5 6) 5
> (if nil 5 6) 6
> (if 4 5 6) 5
The last example is odd but correct: nil means false, and anything else means true. (Unless we have a reason to do otherwise, we use t to mean true, just for the sake of clarity.)
有两个比较特别的 symbol 就是 t 跟 nil 。t 这个 symbol 所定义的值就是 t ,而 nil 这个 symbol 所定义的值就是 nil 。 Lisp 分把把 t 跟 nil 这两个值拿来表示〝真〞跟〝伪〞。一个最典型会用的 t 跟 nil 的例子就是 if 函数,将会更清楚的解释介绍 if 函数。
> (if t 5 6) 5
> (if nil 5 6) 6
> (if 4 5 6) 5
最后一个例子或许会让你感到很奇怪,不过它并没有错误。原因是 nil 表示〝伪〞,而任
何其它的值都表示〝真〞。(除非你有理由要这样写程序,不然通常我们还是习惯用 t 来表示〝真〞,这样读程序的时候也比较清楚。)
QUOTE:
Symbols like t and nil are called self-evaluating symbols, because they evaluate to themselves. There is a whole class of self-evaluating symbols called keywords; any symbol whose name starts with a colon is a keyword. (See below for some uses for keywords.) Some examples:
> :this-is-a-keyword
:THIS-IS-A-KEYWORD > :so-is-this :SO-IS-THIS > :me-too :ME-TOO
像 t 跟 nil 这样经过评估求值之后依然是本身的 symbol 就称做是 self-evaluating symbols (自我评估的 symbol),因为他们经过评估求值之后依然是本身。实际上,还有一大类的 self-evaluating symbols会被称做是 keyword ,就是任何以冒号开头的 symbol 都称做是 keyword 。(皆下来看如何使用 keyword )一些 keyword 例子如下:
> :this-is-a-keyword
:THIS-IS-A-KEYWORD > :so-is-this :SO-IS-THIS > :me-too :ME-TOO
QUOTE:
Numbers(数字)
An integer is a string of digits optionally preceded by + or -. A real number looks like an integer, except that it has a decimal point and optionally can be written in scientific notation. A rational looks like two integers with a / between them. LISP supports complex numbers, which are written #c(r i) (where r is the real part and i is the imaginary part). A number is any of the above. Here are some numbers: 5 17 -34 +6 3.1415 1.722e-15
#c(1.722e-15 0.75)
Numbers
整数型别的定义就是一连串的数字,并且最前方可以选择性的加上+ 或 - 。而实数包含有整数,而且比整数定义广的是,实数还可以有小数点,或是也可以用科学记号表示。有理数则是两个整数相除而得,也就是在两个整数中间加上 / 。 Lisp 还支援复数型别,利用像是 #c(r i) 这样表示复数,其中 r 表示复数的实部,i 表示复数的虚部。上列的任何一种都称做是 Number (数字)型别。 下面是一些 Number 型别的例子: 5 17 -34 +6 3.1415 1.722e-15
#c(1.722e-15 0.75)
QUOTE:
The standard arithmetic functions are all available: +, -, *, /, floor, ceiling, mod, sin, cos, tan, sqrt, exp, expt, and so forth. All of them accept any kind of number as an argument. +, -, *, and / return a number according to type contagion: an integer plus a rational is a rational, a rational plus a real is a real, and a real plus a complex is a complex. Here are some examples: > (+ 3 3/4) ;type contagion 15/4
> (exp 1) ;e 2.7182817
> (exp 3) ;e*e*e 20.085537
> (expt 3 4.2) ;exponent with a base other than e 100.90418
> (+ 5 6 7 (* 8 9 10)) ;the fns +-*/ all accept multiple arguments
There is no limit to the absolute value of an integer except the memory size of your computer. Be warned that computations with bignums (as large integers are called) can be slow. (So can computations with rationals, especially compared to the corresponding computations with small integers or floats.)
对于数字可以做的运算,一些常见的算数函数如 +, -, *, /, floor,ceiling, mod, sin, cos, tan, sqrt, exp, expt 都有内建,而且这些内建的算数函数可以接受任何 Number 型别的参数。 +, -, *, / 这四的函数的传回值型别会随输入参数的型别而自动延伸为较广的型别范围,比如说整数加上有理数的传回值,就会是范围较广的有理数;而有理数加上实数的传回值,是实数;实数加上复数的传回值,则是复数。下面是一些例子:
> (+ 3 3/4) ;传回值型别范围自动加广 15/4
> (exp 1) ;自然对数的基底 e 2.7182817
> (exp 3) ;e*e*e

