Fork me on GitHub

land of lisp 笔记

land of lisp note

by lkl

section 1

chapter 1-2

定义全局变量:
(defparameter small 1)
(defvar small 2)

定义局部变量:

1
2
3
4
5
6
(let (variable declarations)
...body...)
> (let ((a 5)
(b 6))
(+ a b))
11

ash(arithmetic shift functionv),算数位移方法,

1
2
3
4
5
> (ash 11 1)
22

> (ash 11 -1)
5

1
2
3
4
> (let ((a 5)
(b 6))
(+ a b))
11

chapter 3

定义全局函数

1
2
> (defun function_name (arguments) 
...)

定义局部函数

1
2
3
4
5
6
7
8
(flet ((function_name (arguments)       
...function body...))
...body...)

> (flet ((f (n)
(+ n 10)))
(f 5))
15

比较两个符号是否相同:

1
2
> (eq 'fooo 'FoOo)
T

求幂:

1
2
> (expt 53 53)
24356848165022712132477606520104725518533453128685640844505130879576720609150223301256150373

显示字符串:

1
2
3
> (princ "Tutti Frutti")
Tutti Frutti
"Tutti Frutti"

链接任何两个数据(无论类型):

1
2
3
4
5
6
> (cons 'chicken 'cat)
(CHICKEN . CAT)
> (cons 'pork (cons 'beef (cons 'chicken ())))
(PORK BEEF CHICKEN)
> (cons 'pork '(beef chicken))
(PORK BEEF CHICKEN)

从第一个cell中取出元素:

1
2
> (car '(pork beef chicken))
PORK

从第二个cell或剩下所有cell中取出元素:

1
2
> (cdr '(pork beef chicken))
(BEEF CHICKEN)

可以将car和cdr串联到cadr,cdar或cadadr等新功能中。 可以简洁地从复杂列表中提取特定数据。 输入cadr与使用car和cdr一样,它从列表中返回第二项。 (第二个cons单元格的第一个cell将包含该项目。)看一下这个例子:

1
2
3
4
5
6
7
8
> (cdr '(pork beef chicken))
(BEEF CHICKEN)
> (car '(beef chicken))
BEEF
> (car (cdr '(pork beef chicken)))
BEEF
> (cadr '(pork beef chicken))
BEEF

在这里注意:

1
2
> (cddddr '(pork beef chicken fish apple orange ))
(APPLE ORANGE)

c*r最多可达四级。

这里成功取出了最后两个元素,但是当按照同样的方法取最后一个元素时:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> (cdddddr '(pork beef chicken fish apple orange ))

*** - EVAL: undefined function CDDDDDR
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of (FDEFINITION 'CDDDDDR).
RETRY :R2 Retry
STORE-VALUE :R3 Input a new value for (FDEFINITION 'CDDDDDR).
ABORT :R4 Abort debug loop
ABORT :R5 Abort debug loop
ABORT :R6 Abort debug loop
ABORT :R7 Abort debug loop
ABORT :R8 Abort debug loop
ABORT :R9 Abort debug loop
ABORT :R10 Abort debug loop
ABORT :R11 Abort debug loop
ABORT :R12 Abort debug loop
ABORT :R13 Abort debug loop
ABORT :R14 Abort debug loop
ABORT :R15 Abort debug loop
ABORT :R16 Abort debug loop
ABORT :R17 Abort main loop

构建列表:

1
2
> (list 'pork 'beef 'chicken)
(PORK BEEF CHICKEN)

以下三种创建列表的方式没有区别:

1
2
3
4
5
6
> (cons 'pork (cons 'beef (cons 'chicken ())))
(PORK BEEF CHICKEN)
> (list 'pork 'beef 'chicken)
(PORK BEEF CHICKEN)
> '(pork beef chicken)
(PORK BEEF CHICKEN)

section 2

chapter 4