Common LISP Hints
Geoffrey J. Gordon
Friday, February 5, 1993
Modified by
Bruno Haible
<haible@ma2s2.mathematik.uni-karlsruhe.de>
简体中文版翻译:
刘鑫
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时可能会有细节上的区别。
Further Information
附:
The best LISP textbook I know of is
Guy L. Steele Jr. _Common LISP: the Language_. Digital Press. 1984.
据我所知最好的 Lisp 书籍是:
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.
我还记得 Dave Touretsky 写了一本,不过我从来没读过,所以不能对那本书
发表评论。
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:
符号仅仅是字符串。你可以在符号中包含字母、数字、连接符等等,唯一的限制就
是要以字母开头。(如果你只输入数字,最多再以一个连接符开头的话,LISP会认
为你输入了一个整数而不是符号。)例如:
a
b
c1
foo
bar
baaz-quux-garply
Some things you can do with symbols follow. (Things after a ">" prompt
are what you type to the LISP interpreter, while other things are what
the LISP interpreter prints back to you. The ";" is LISP's comment
character: everything from a ";" to the end of line is ignored.)
接下来我们可以做些事情。(“>”标记表示你向LISP输入的东西,其它的是LISP
打印返回给你的。“;”是LISP的注释符:“;”后面的整行都会被忽略。)
> (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
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:
有两个特殊的符号, t 和 nil 。 t 的值总是定义为 t ,nil 的值总是定义为
nil 。LISP用 t 和 nil 代表 true 和 false。以下是使用这个功能的 if 语句
,后面再做详细说明:
> (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.)
最后一个例子看起来很怪,但是没有错:nil 代表 false ,其它任意值代表 true。
(为了代码清晰,在没有什么特别原因的情况下,我们用t代表true。)
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:
t 和 nil 这样的符号被称为自解析符号,因为他们解析为自身。自解析符号称为
关键字;任一以冒号开头的符号都是关键字。(下面是一些关键字的应用)如下
所示:
> :this-is-a-keyword
:THIS-IS-A-KEYWORD
> :so-is-this
:SO-IS-THIS
> :me-too
:ME-TOO
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:
数值类型是数字文本,可能会以 + 或 - 开头。实数和整数很相像,但是它带有
小数点,还可能写成科学计数法。有理数就像是两个整数之间带有一个/。LISP支
持复数,写为#c(r i)(r表示实部,i表示虚部)。以上统称为数值。下面是一
些数值:
5
17
-34
+6
3.1415
1.722e-15
#c(1.722e-15 0.75)
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:
标准的计算函数包括: +, -, *, /, floor, ceiling, mod, sin, cos, tan,
sqrt, exp, expt 等等。所有这些函数都可以接受任意数值类型参数。+、-、* 和
/返回尽可能大的类型:一个整数加一个有理数返回有理数,一个有理数加一个实数
是一个实数,一个实数加一个复数是一个复数。如下所示:
> (+ 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.)
对于整数来说,唯一的大小限制就是机器的内存。当然大数值运算(这
会调用大整数)可能会很慢。(因此我们可以计算有理数,尤其是小整数和浮点数
的比较运算)
Conses
A cons is just a two-field record. The fields are called "car" and
"cdr", for historical reasons. (On the first machine where LISP was
implemented, there were two instructions CAR and CDR which stood for
"contents of address register" and "contents of decrement register".
Conses were implemented using these two registers.)
cons 就是一个包含两个字段的记录。出于历史原因,两个字段分别被称为
“car”和“cdr”。(在第一台实现LISP的机器上,用CAR和CDR代表“地址寄
存器的内容”和“指令寄存器的内容”。Conses的实现主要依靠这两个寄存器。)
Conses are easy to use:
Conses很容易使用:
> (cons 4 5) ;Allocate a cons. Set the car to 4 and the cdr to 5.
(4 . 5)
> (cons (cons 4 5) 6)
((4 . 5) . 6)
> (car (cons 4 5))
4
> (cdr (cons 4 5))
5
Lists
You can build many structures out of conses. Perhaps the simplest is a
linked list: the car of each cons points to one of the elements of the
list, and the cdr points either to another cons or to nil. You can
create such a linked list with the list fuction:
你可以构造conses之外的结构。可能最简单的是链表:每一个cons的car指向链表
的一个元素,cdr指向另一个cons或者nil。我们可以使用list函数构造链表。
> (list 4 5 6)
(4 5 6)
Notice that LISP prints linked lists a special way: it omits some of
the periods and parentheses. The rule is: if the cdr of a cons is nil,
LISP doesn't bother to print the period or the nil; and if the cdr of
cons A is cons B, then LISP doesn't bother to print the period for cons
A or the parentheses for cons B. So:
需要注意的是 LISP 用一种特殊的方式打印链表:它忽略掉某些分隔和括号,
规则如下:如果某个 cons 的 cdr 是 nil ,LISP 不打印 nil 和段标记,如果
cons A 的 cdr 是 cons B,LISP不打印 cons B 的括号和 cons A 的分隔符。
如下:
> (cons 4 nil)
(4)
> (cons 4 (cons 5 6))
(4 5 . 6)
> (cons 4 (cons 5 (cons 6 nil)))
(4 5 6)
The last example is exactly equivalent to the call (list 4 5 6). Note
that nil now means the list with no elements: the cdr of (a b), a list
with 2 elements, is (b), a list with 1 element; and the cdr of (b), a
list with 1 element, is nil, which therefore must be a list with no
elements.
最后一个例子相当于调用(list 4 5 6)。要注意的是这里 nil 表示没有元素的空
链表:包含两个元素的链表(a b)中,cdr是(b),一个含有单个元素的链表;包含
一个元素的链表(b),cdr是nil,故此这里必然是一个没有元素的链表。
The car and cdr of nil are defined to be nil.
nil 的 car 和 cdr 定义为nil。
If you store your list in a variable, you can make it act like a stack:
如果你把链表存储在变量中,可以将它当作堆栈来使用:
> (setq a nil)
NIL
> (push 4 a)
(4)
> (push 5 a)
(5 4)
> (pop a)
5
> a
(4)
> (pop a)
4
> (pop a)
NIL
> a
NIL
Functions
You saw one example of a function above. Here are some more:
前面我们讨论过一些函数的例子,这里还有更多:
> (+ 3 4 5 6) ;this function takes any number of arguments
18
> (+ (+ 3 4) (+ (+ 4 5) 6)) ;isn't prefix notation fun?
22
> (defun foo (x y) (+ x y 5)) ;defining a function
FOO
> (foo 5 0) ;calling a function
10
> (defun fact (x) ;a recursive function
(if (> x 0)
(* x (fact (- x 1)))
1))
FACT
> (fact 5)
120
> (defun a (x) (if (= x 0) t (b (- x)))) ;mutually recursive functions
A
> (defun b (x) (if (> x 0) (a (- x 1)) (a (+ x 1))))
B
> (a 5)
T
> (defun bar (x) ;a function with multiple statements in
(setq x (* x 3)) ;its body -- it will return the value
(setq x (/ x 2)) ;returned by its final statement
(+ x 4))
BAR
> (bar 6)
13
When we defined foo, we gave it two arguments, x and y. Now when we
call foo, we are required to provide exactly two arguments: the first
will become the value of x for the duration of the call to foo, and the
second will become the value of y for the duration of the call. In
LISP, most variables are lexically scoped; that is, if foo calls bar
and bar tries to reference x, bar will not get foo's value for x.
当我们定义函数的时候,设定了两个参数,x 和 y。现在当我们调用 foo,需要
给出两个参数:第一个在 foo 函数调用时成为 x 的值,第二个成为 y 的值。
在LISP中,大部分的变量都是局部的,如果 foo 调用了 bar ,bar中虽然使用了
名字为x的引用,但bar 得不到 foo 中的 x 。
The process of assigning a symbol a value for the duration of some
lexical scope is called binding.
在调用过程中给一个符号赋值的操作被称为绑定。
You can specify optional arguments for your functions. Any argument
after the symbol &optional is optional:
我们可以给函数指定可选参数,在符号&optional 之后的参数是可选参数:
> (defun bar (x &optional y) (if y x 0))
BAR
> (defun baaz (&optional (x 3) (z 10)) (+ x z))
BAAZ
> (bar 5)
0
> (bar 5 t)
5
> (baaz 5)
15
> (baaz 5 6)
11
> (baaz)
13
It is legal to call the function bar with either one or two arguments.
If it is called with one argument, x will be bound to the value of that
argument and y will be bound to nil; if it is called with two
arguments, x and y will be bound to the values of the first and second
argument, respectively.
bar函数的调用规则是要给出一个或两个参数。如果它用一个参数调用,x 将会绑
定到这个参数值上,而 y 就是 nil;如果用两个参数调用它,x和y会分别绑定
到第一和第二个值上。
The function baaz has two optional arguments. It specifies a default
value for each of them: if the caller specifies only one argument, z
will be bound to 10 instead of to nil, and if the caller specifies no
arguments, x will be bound to 3 and z to 10.
baaz 函数有两个可选参数。它为它们分别提供了默认值:如果调用者只给出了一
个参数,z会绑定为10而不是nil,如果调用者没有给出参数,x会绑定为3,而z绑
定为10。
You can make your function accept any number of arguments by ending its
argument list with an &rest parameter. LISP will collect all arguments
not otherwise accounted for into a list and bind the &rest parameter to
that list. So:
在参数列表的最后设置一个 &rest 参数,可以使我们的函数接受任意数目的参数。
LISP把所有的附加参数都放进一个链表并绑定到 &rest 参数。如下:
> (defun foo (x &rest y) y)
FOO
> (foo 3)
NIL
> (foo 4 5 6)
(5 6)
Finally, you can give your function another kind of optional argument
called a keyword argument. The caller can give these arguments in any
order, because they're labelled with keywords.
最后,我们可以为函数指定一种被称为关键字参数的可选参数。调用者可以用
任意顺序调用这些参数,因为他们已经通过关键字标示出来。
> (defun foo (&key x y) (cons x y))
FOO
> (foo :x 5 :y 3)
(5 . 3)
> (foo :y 3 :x 5)
(5 . 3)
> (foo :y 3)
(NIL . 3)
> (foo)
(NIL)
An &key parameter can have a default value too:
关键字参数也可以有默认值:
> (defun foo (&key (x 5)) x)
FOO
> (foo :x 7)
7
> (foo)
5
Printing
Some functions can cause output. The simplest one is print, which
prints its argument and then returns it.
某些函数可以用来输出。最简单的一个是 print,它可以打印参数并且返回
它们。
> (print 3)
3
3
The first 3 above was printed, the second was returned.
首先打印3,然后返回它。
If you want more complicated output, you will need to use format.
Here's an example:
如果你需要更复杂的输出,可能会用到 format,这里有个例子:
> (format t "An atom: ~S~%and a list: ~S~%and an integer: ~D~%"
nil (list 5) 6)
An atom: NIL
and a list: (5)
and an integer: 6
The first argument to format is either t, nil, or a stream. T specifies
output to the terminal. Nil means not to print anything but to return a
string containing the output instead. Streams are general places for
output to go: they can specify a file, or the terminal, or another
program. This handout will not describe streams in any further detail.
第一个参数可以是 t,nil 或者一个流。t意味着输出到终端;nil意味着不打印
任何东西,而是把它返回。流是用于输出的通用方式:它可以是一个指定的文件,
或者一个终端,或者另一个程序。这里不再详细描述流的更多细节。
The second argument is a formatting template, which is a string
optionally containing formatting directives.
第二个参数是个格式化模版,即一个包含格式化设定的字符串。
All remaining arguments may be referred to by the formatting
directives. LISP will replace the directives with some appropriate
characters based on the arguments to which they refer and then print
the resulting string.
所有其它的参数由格式化设定引用。LISP会根据标示所引用的参数,将其替换
为合适的字符,并返回结果字符串。
Format always returns nil unless its first argument is nil, in which
case it prints nothing and returns a string.
如果format的第一个参数是 nil ,它返回一个字符串,什么也不打印,否则
它总是返回 nil 。
There are three different directives in the above example: ~S, ~D, and
~%. The first one accepts any LISP object and is replaced by a printed
representation of that object (the same representation which is
produced by print). The second one accepts only integers. The third one
doesn't refer to an argument; it is always replaced by a carriage
return.
前面的例子中有三种不同的标示:~S,~D和~%。第一个接受任意LISP对象并且将
其替换为这个对象的打印描述(与使用print打印出的描述信息相同)。第二个
Another useful directive is ~~, which is replaced by a single ~.
另一个常用的标示是~~,它替换为单个~。
Refer to a LISP manual for (many, many) additional formatting
directives.
LISP手册中介绍了其它(很多,很多)的格式化标示。