Вы находитесь на странице: 1из 449

- -

Books.Ru -
ISBN 978-5-93286-206-3, ANSI Common Lisp
- Books.Ru . -
- ,
-
. ,
- (piracy@symbol.ru),
.
ANSI
Common Lisp

Paul Graham

PRENTICE HALL
ANSI
Common Lisp

-
2012
High tech

ANSI Common Lisp
.
.
. .
.
.
.
.

.
ANSI Common Lisp. . . .: -, 2012. 448 ., .
ISBN 978-5-93286-206-3
ANSI Common Lisp
ANSI- .
.
.
,
,
, ,
, . -
, ,
.
: - -
CLOS, -
- ; 20 ,
, ,
, , HTML,
, -, , -
. -
, , , ,
, , -
, , , , -
, .
,
, , -
-, -
.

ISBN 978-5-93286-206-3
ISBN 0-13-370875-6 ()
-, 2012
Authorized translation of the English edition 1996 Prentice Hall, Inc. This trans-
lation is published and sold by permission of Prentice Hall, Inc., the owner of all rights
to publish and sell the same.
, -
. -
, , .
-. 199034, -, 16 , 7,
. (812) 380-5007, www.symbol.ru. N 000054 25.12.98.
03.10.2012. 70100 1/16.
. 28 . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.12. . . . . . . . . . . . . . . . . . . . . . . . . 39
2.13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.5. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6

3.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.9. , . . . . . . . . . . . . . . 58
3.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.15. : . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4. . . . . . . . . . . . . . . . . . . . . . 73
4.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.2. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.5. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.7. : . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.8. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
5.7. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
6.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
6.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
6.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.4. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
6.6. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
6.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
6.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
6.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
7

7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
7.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
7.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
7.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
7.4. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
7.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
8.3. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
8.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
8.5. . . . . . . . . . . . . . . . . . . . . . . . . . 147
8.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
8.8. : . . . . . . . . . . . . . . . . . . . . . . . 149
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
9.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
9.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
9.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
9.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
9.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
9.8. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
10.1. Eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
10.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
10.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
10.4. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
10.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
10.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
10.7. : - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
10.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
8

11. CLOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185


11.1. - . . . . . . . . . . . . . . 185
11.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
11.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
11.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
11.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
11.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
11.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
11.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
11.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
11.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
12.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
12.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
12.3. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
12.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
12.5. : . . . . . . . . . . . . . . . . . . . . . . . . . 209
12.6. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
12.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
13.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
13.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
13.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
13.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
13.5. : . . . . . . . . . . . . . . . . . . . . . . . 232
13.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
13.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
14.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
14.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
14.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
14.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
14.5. Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
14.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
9

15. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
15.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
15.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

16. : HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263


16.1. HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
16.2. HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
16.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
16.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

17. : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
17.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
17.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
17.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
17.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
17.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
17.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
17.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
17.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
B. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
C. Common Lisp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Half lost on my firmness gains to more glad heart,
Or violent and from forage drives
A glimmering of all sun new begun
Both harp thy discourse they marchd,
Forth my early, is not without delay;
For their soft with whirlwind; and balm.
Undoubtedly he scornful turnd round ninefold,
Though doubled now what redounds,
And chains these a lower world devote, yet inflicted?
Till body or rare, and best things else enjoyd in heavn
To stand divided light at evn and poise their eyes,
Or nourish, likning spiritual, I have thou appear. 1

Henley

, ,
,
,
,
, ;
, .
,
, ,
, ?

, , ,
, , .

Henley

1
Henley
. , -
8. . .

Common
Lisp. : -
Common
Lisp, ANSI Common
Lisp, .

ANSI Common Lisp , -


, .
.
, , , . -
, -
,
, .

,
. ,
, , . ,
,
ANSI Common Lisp.


. ,
. -
, .
2 ,
-. 39 -
. -
, , -
.
, -
.
1014 , CLOS ( Common
14

Lisp), , , -
, . 1517
:
, HTML-
- .
,
. AD
, 58 ,
ANSI Common Lisp -
ANSI Common Lisp.
, , -
, .
: .

ANSI Common Lisp,


Common Lisp. , -
, , -
, -
.
, , -
,
:
http://www.eecs.harvard.edu/onlisp/
ftp:
ftp://ftp.eecs.harvard.edu:/pub/onlisp/
pg@eecs.harvard.edu.

On Lisp
,
, -
. , -
,
. , -
,
. ,
, -
On Lisp1, -
.

1
Paul Graham On Lisp, Prentice Hall, 1993. . .
15

, , -
,
. ,
Henley (c. 149) (. 254),
.
-
: (Skona Brittain), (John Fo-
deraro), (Nick Levine), (Peter Norvig)
(Dave Touretzky).
, . -
5.7.
,
, (Ken Anderson), (Tom
Cheatham), (Richard Fateman), (Steve
Hain), (Barry Margolin), (Waldo Pache-
co), (Wheeler Ruml) (Stuart Russel).
.

.
: (Tony Hartman), -
(Dave Mazieres), (Janusz Juda), -
(Harry Bochner) (Joanna Klys).
, -
Prentice Hall: (Alan Apt), (Mona
Pompili), (Shirley McGuire) (Shirley
Michaels).
(Gino Lee) Bow & Arrow Press, .
LATEX, , -
(Leslie Lamport) TEX (Donald Knuth),
..
(L.A. Carr), (Van Jacobson) (Guy Steele). -
Idraw, -
(John Vlissids) (Skott Stanton).
Ghost-
view, (Tim Theisen) -
Ghostscript, . (L. Peter Deutch).
: (Henry
Baker), (Kim Barrett), (Ingrid Basset),
(Trevor Blackwell), (Paul Becker), -
(Gary Bisbee), (Frank Dueschmann), -
(Frances Dickey), (Rich and Scott
Draves), (Bill Dubuque), (Dan Friedman),
16

(Jenny Graham), (Alice Hartley),


(David Hendler), (Mike Hewlett), -
(Glenn Hollowat), (Brad Karp), (Sonya
Keene), (Ross Knights), (Mutsumi Komu-
ro), (Steffi Kutzia), (David Kuznick), -
(Madi Lord), (Julie Mallozzi),
(Paul McNamee), (Dave Moon), (Howard
Mullings), (Mark Nitzberg), (Nancy Par-
met) , (Robert Penny), (Mike
Plusch), (Cheryl Sacks), (Hazem Sayed),
(Shannon Spires), (Lou Steinberg),
(Paul Stoddard), (John Stone),
(Guy Steele), (Steve Strassmann), (Jim
Veitch), (Dave Watkins),
(Idelle and Julian Weber), (the Weickers), (Dave
Yost) (Alan Yuille).
.

-
. ,
, -
.
, -
. ,
. -
, . -
.
, , -
. -.
- . -
. -
. , .

, , 1996 ,
. 15 .
, -
, , -
, .
,
. ,
, , -
. , -
,
.
, , -
, . , -
15 , -
, , -
.
-
.
-
, ,
. -
, , C++, , (
). -
.
, -
. , ,
Perl, Python, -
, , -
Ruby, .
,
. , Perl
Unix- . -
, , -
, . , -
. ,
18


, -
.
,
. -
- .
, .
Common Lisp. -
,
. , -
. , -
Common Lisp, , .
,
,
. -
, .
(, -
), -
, . ,
,
. , -
Slime1,
.

, , ,
-
.

1
http://common-lisp.net/project/slime/.
1
1.

-
1958 . ,
. ,
. ,
, , ,
.
-
. ,
, (-
, - ),
. ,
.

1.1.
? ,
. ,
, n, -
:
; Lisp /* C */
(defun sum (n) int sum(int n){
(let ((s 0)) int i , s = 0;
(dotimes (i n s) for(i = 0; i < n; i++)
(incf s i)))) s += i;
return(s);
}
, , -
, . ,
20 1.

, n
, n :
; Lisp
(defun addn (n)
#(lambda (x)
(+ x n)))
addn ? -
.
, , , ? -
, .
, -
, , .
, -
, ,
.
, ?
( -
), -
, - .
, . -
6.7 .
, -
. , , -
, , ,
. , ,
. ?
, ,
. , , 10.
, -
- . -
, , , -
. ,
17.
213 ,
17. -
C++ , -
C++ . , -
, . , , -
. C++,
C++ ,
. ,
. -
-, .
1.2. 21

1.2.
, ,
. . , -
, , -
, , -
. , ,
.

. ,
, .
,
. ( ),
. , -
,
. - (bottom-up).
,
, , -
-. -
, - -
. ,
, TEX.
- , .
- .
, -
. -
, 80- -
: GNU Emacs, Autocad Interleaf.
, , , -
.
, .
-
,
, .

,
. ,
,
?
,
. ,
, . -
, -
.
22 1.

,
, .

.
. -
, . , -
,
, , .
,
, , -
. - ? -
? -
? ?
,
, .

1.3.
, -
,
. , -
.

, .
. ,
, -
. . , -
, -
- . ,
.
.
OS/360
, :

OS/360 ,
. , ,
, -
, ,
.

, .
, .
,
- -
. , -
1.3. 23

. , -
, .
, . -
,
.
, .
-
. , -
, -
.
. : ,
.
, . -
-
, , .
1960 .
, -
, -
. -
,
. -
.
( , ) -
, , -
. , -
,
, .
, -
. , -
, . , -
. ,
, -
, .
,
. -
. .
,
.
-
. , . :


. -
,
.
24 1.

, ,
, , -
.
.
- ;
, . , -
, -
. ,
,
. -
, , ,
-. , -
(, , ,
), .
. -
, ,
.
, , , -
. -
, .
2
2.

-
. , Common Lisp,
.

2.1.
,
. -
, (toplevel). -
toplevel, .
, ,
. -
>. .
-
. 1 ,
> 1
1
>
, -
, .
, -
. .
(, 1) . -
,
. , ,

26 2.

> (+ 2 3)
5
(+ 2 3) + , 2 3 .
2+3, -
+, ,
: (+ 2 3).
, -
. ,
-
.
, ,
:
2 + 3 + 4
:
(+ 2 3 4)
1 + ,
. , -
:
> (+)
0
> (+ 2)
2
> (+ 2 3)
5
> (+ 2 3 4)
9
> (+ 2 3 4 5)
14
, -
, .
.
:
> (/ ( 7 1) ( 4 2))
3
7 1 4 2.
: -
(, 1), , -
. -:
2 (+ 2 3) (+ 2 3 4) (/ ( 7 1) ( 4 2))

1
. . .
2.2. 27

, .
:
,
, -
, -
, .
.

2.2.
toplevel, -
. -
.
+ , (+ 2 3) -
. 2 :
1. , .
,
2 3.
2. , .
, 5.
, -
. -
. , (/ ( 7 1)
( 4 2)):
1. ( 7 1): 7 7, 1 1. -
-, 6.
2. ( 4 2): 4 4, 2 2, -
2.
3. 6 2 /, 3.
Common Lisp , . -
.
, -
. -
Common Lisp.
,
Common Lisp . quote,
. quote ; , -
, : .
quote -
:
> (quote (+ 3 5))
(+ 3 5)
28 2.


-, , -
, toplevel, -
(break loop). -
,
, .
Common Lisp --
. :abort.
> (/ 1 0)
Error: Division by zero.
Options: :abort, :backtrace
>> :abort
>
A , ,
.

Common Lisp quote -


. , -
:
> (+ 3 5)
(+ 3 5)
quote , -
.
-
. , -
.

2.3.
, -
, , - .
. integer ,
: 256. ,
, ,
, :
"ora et labora". , .
, ,
. .
, :
> Artichoke
ARTICHOKE
2.3. 29

, , , -
, , , -
.
, -
. ,
.
, :
> (my 3 "Sons")
(MY 3 "Sons")
> (the list (a b c) has 3 elements)
(THE LIST (A B C) HAS 3 ELEMENTS)
,
, .
list. -
, .
list +:
> (list my (+ 2 1) "Sons")
(MY 3 "Sons")
.
, , .

, , ,
.
, , -,
, -
.
10,
.
. , -
. -
:
> (list (+ 2 1) (+ 2 1))
((+ 2 1) 3)
,
.
. Common Lisp -
:
nil. , , -
nil.
> ()
NIL
> nil
NIL
30 2.

() , nil -
.

2.4.
cons.
, -
, :
> (cons a (b c d))
(A B C D)
cons
. list, ,
cons.
> (cons a (cons b nil))
(A B)
> (list a b)
(A B)
car
cdr. car , cdr
, :
> (car (a b c))
A
> (cdr (a b c))
(B C)
car cdr, -
. , , :
> (car (cdr (cdr (a b c d))))
C

third:
> (third (a b c d))
C

2.5.
Common Lisp t.
nil, t . ,
listp , :
> (listp (a b c))
T
2.5. 31

, ,
. Common Lisp
p.
Common Lisp nil, .
listp , , nil:
> (listp 27)
NIL
nil Common Lisp, null, -
:
> (null nil)
T
not, , -
:
> (not nil)
T
.
Common Lisp if. -
: test-, then- else-.
test-. , then--
() . -
else- ().
> (if (listp (a b c))
(+ 1 2)
(+ 5 6))
3
> (if (listp 27)
(+ 1 2)
(+ 5 6))
11
quote, if , ,
, if
.
if . ,
nil.
> (if (listp 27)
(+ 2 3))
NIL
, t, -
, nil, :
> (if 27 1 2)
1
32 2.

and () or () .
,
, , . -
( nil), and -
:
> (and t (+ 1 2))
3
,
. or, -
,
.
. ,
. 10 ,
.

2.6.
defun. -
: ,
, .
defun third:
> (defun our-third (x)
(car (cdr (cdr x))))
OUR-THIRD
, our-third.
, (x), ,
: x. x -
. , x
, .
, (car (cdr (cdr x))), .
, , -
. our-third (car (cdr (cdr x))),
x :
> (our-third (a b c d))
C
, , ,
. , -
. , ,
. -
, -
.
-. -
, 1 4 3:
2.7. 33

> (> (+ 1 4) 3)
T
, -
, , -
:
> (defun sum-greater (x y z)
(> (+ x y) z))
SUM-GREATER
> (sum-greater 1 4 3)
T
, .
( ).
1, -
toplevel. , ,

.

2.7.
, ,
, . , sum-grea-
ter + >. ,
.
, , . Com-
mon Lisp member, , -
- . :
(defun our-member (obj lst)
(if (null lst)
nil
(if (eql (car lst) obj)
lst
(our-member obj (cdr lst)))))
eql .
.
> (our-member b (a b c))
(B C)
> (our-member z (a b c))
NIL
, . , obj
lst, :

1
, , , main. .
.
34 2.

1. , lst. , , obj -
.
2. obj lst, , -
.
3. , obj
lst.
, -
, .
.
. ,
, -
, -
. -
: -
, ?
. -
.
. , -
, -
.
:
1. .
2. .
3. ,
, .
, , ,
-
.
our-member ,
- .
, .
, , -
, .

2.8.

. -
- . , -
. ,
, ?
?
: . - -
, , .
2.9. 35

, --
, . -
, , -
, - .1
-
. ,

, .
-, , -
our-member, :
(defun our-member (obj lst) (if (null lst) nil (if (eql (car lst) obj) lst
(our-member obj (cdr lst)))))
,
:
defun our-member (obj lst)
if (null lst)
nil
if eql (car lst) obj
lst
our-member obj (cdr lst)
,
-
.

2.9.
- toplevel. -
- , -
. -.
Common Lisp format.
: , -
, ,
, -
. :
> (format t "~A plus ~A equals ~A.~%" 2 3 (+ 2 3))
2 plus 3 equals 5.
NIL
, .
format, toplevel.
format toplevel,
, .

1
vi :set sm. Emacs M-x lisp-
mode .
36 2.

format, t, ,
, .
toplevel. , -
. ~A , ~%
. -
.
read (ee ).
, -
, toplevel. , -
:
(defun askem (string)
(format t "~A" string)
(read))
:
> (askem "How old are you? ")
How old are you? 29
29
, read
Enter. read, -
, , -
, .
, read: -
. --
.
-
. read
.
, askem ,
-
. , -
. , -
.
,
. ,
- . -
(+ 1 2) 3, -
. -
format,
, - . -
.
,
, -
, -
,
2.10. 37

. ,
, .

2.10.
Common Lisp
let, :
> (let ((x 1) (y 2))
(+ x y))
3
let . -
, . -
.
, x y, -
1 2. let.
, -
. -
, (+ x y). let -
. askem
let:
(defun ask-number ()
(format t "Please enter a number. ")
(let ((val (read)))
(if (numberp val)
val
(ask-number))))
val, read. -
, , .
, numberp , ,
.
, ask-number , -
. ,
read :
> (ask-number)
Please enter a number. a
Please enter a number. (no hum)
Please enter a number. 52
52
val, let, -
, .
, .1

1

6.
38 2.

def-
parameter:
> (defparameter *glob* 99)
*GLOB*
, ,
. -
,
, .
,
defconstant1:
(defconstant limit (+ *glob* 1))
, -
, -
. , -
, boundp:
> (boundp *glob*)
T

2.11.
Common Lisp -
setf. -
:
> (setf *glob* 98)
98
> (let ((n 10))
(setf n 2)
n)
2
setf ,
,
:
> (setf x (list a b c))
(A B C)
, ,
. -
defpara-
meter.

1
, -
,
+. , -
+limit+. . .
2.12. 39

, -
. setf ,
.
, :
> (setf (car x) n)
N
> x
(N B C)
setf ,
- . -
D setf.
setf () . -

(setf a b
c d
e f)
setf:
(setf a b)
(setf c d)
(setf e f)

2.12.
,
, ,
. . -
.
, remove
, , ,
.
> (setf lst (c a r a t))
(C A R A T)
> (remove a lst)
(C R T)
, remove ? -
. :
> lst
(C A R A T)
? -
- -
setf. -
a x, :
(setf x (remove a x))
40 2.

setf
. ,
, , .
?
, . , -
, , ,
, .
, .
-
.
, , -
, , -
, .
, -
.
,
.

2.13.
- , -
, . -
.
(defun show-squares (start end)
(do ((i start (+ i 1)))
((> i end) done)
(format t "~A ~A~%" i (* i i))))
start end:
> (show-squares 2 5)
2 4
3 9
4 16
5 25
DONE
do Common Lisp.
let, do .
:
(variable initial update)
variable , initial update . -
initial,

update. show-squares do , i.
i start,
.
2.13. 41

do , -
, , .
(> i end). -
-
,
do. show-squares done.
do . -
. -
,
, ( )
.
show-squares:
(defun show-squares (i end)
(if (> i end)
done
(progn
(format t "~A ~A~%" i (* i i))
(show-squares (+ i 1) end))))
progn. -
, -
.
Common Lisp -
. , , -
:
(defun our-length (lst)
(let ((len 0))
(dolist (obj lst)
(setf len (+ len 1)))
len))
dolist (vari-
able expression) . -
, -
. obj lst -
len.
:
(defun our-length (lst)
(if (null lst)
0
(+ (our-length (cdr lst)) 1)))
, 0, 1
cdr. our-length , -
, (. 13.2).
42 2.

2.14.
, -
, . function, -
. quote, function -
, :
> (function +)
#<Compiled-Function + 17BA4E>
-
Common Lisp.
, -
, . -
. +
. Common Lisp
.
quote -
# function:
> #+
#<Compiled-Function + 17BA4E>
, . -
, , apply.

:
> (apply #+ (1 2 3))
6
> (+ 1 2 3)
6
Apply ,
:
> (apply #+ 1 2 (3 4 5))
15
funcall , ,
:
> (funcall #+ 1 2 3)
6

defun.
, defun.
,
.
,
. ,
2.14. 43

-. - , -
lambda ,
0 . -, -
,
:
(lambda (x y)
(+ x y))
(x y) , .
- .
, - -
:
> ((lambda (x) (+ x 100)) 1)
101
# ,
:
> (funcall #(lambda (x) (+ x 100))
1)
101
, , -
.

?
- lambda .
. :
,

, lambda.
Common Lisp ,
, -
lambda . -
, , :
((x) (+ x 100))

(lambda (x) (+ x 100))
-
lambda, Common Lisp .
44 2.

2.15.
. -

. Common Lisp , -
. , , -
. .
, -
.
, ,
.
13.3.
Common Lisp -
. . , 27 -
fixnum, integer, rational, real, number, atom t -
. ( 9.) t -
, -
t.
typep , -
, :
> (typep 27 integer)
T
Common Lisp
.

2.16.
.
-
. , -
. ,
. , -,
. -
, -
, ,
.
,
. ,
. : -
.

UNIX. -
. -
. , , -
45

, -
. :
,
.
, .


1. . toplevel, -
.
2. .
,
.
.
3. Common Lisp : -
,
. quote -
( ).
4. .
,
, .
5. : cons, -
; car, ; cdr, -
, .
6. Common Lisp t , nil .
, nil, -
. if. and or -
.
7. .
defun.
8. , , .
, -
.
9. ,
- .
10. -: read,
-, format,
.
11. let, -
defparameter.
12. setf.
, .
46 2.

13. -
, .
14. do.
15. . -
-, -
.
16. , .

1. , :
(a) (+ (- 5 1) (+ 3 7))
(b) (list 1 (+ 2 3))
(c) (if (listp 1) (+ 1 2) (+ 3 4))
(d) (list (and (listp 3) t) (+ 1 2))
2. cons ,
(a b c).
3. car cdr , -
.
4. , -
.
5. ?
(a) (defun enigma (x)
(and (not (null x))
(or (null (car x))
(enigma (cdr x)))))

(b) (defun mystery (x y)


(if (null y)
nil
(if (eql (car y) x)
0
(let ((z (mystery x (cdr y))))
(and z (+ z 1))))))
6. x ?
(a) > (car (x (cdr (a (b c) d))))
B

(b) > (x 13 (/ 1 0))


13

(c) > (x #list 1 nil)


(1)
47

7. , ,
. , -
.
8. , -
:
(a) , -
;
(b) a .
9. ,
, nil.
, . , -
, :
(a) (defun summit (lst)
(remove nil lst)
(apply #+ lst))

(b) (defun summit (lst)


(let ((x (car lst)))
(if (null x)
(summit (cdr lst))
(+ x (summit (cdr lst))))))
3
3.

. -
,
: LISt Processor ( ).
. Common Lisp

.
- -
.
,
-
.
, -
.

3.1.
2.4 cons, car cdr -
. , cons -
, (cons). , cons
, car, cdr.
cons- ,
.
cons .
cons-, -
, .
, -
. . -
car ,
3.1. 49

cdr ( cons-, nil). -


: car
, cdr . -
first rest. ,
,
cons-.
cons nil,
> (setf x (cons a nil))
(A)
, , . 3.1.
, -
, car
cdr. car cdr, ,
:
> (car x)
A
> (cdr x)
NIL

nil

. 3.1. ,

, :
> (setf y (list a b c))
(A B C)
. 3.2. cdr
:
> (cdr y)
(B C)

nil

a b c

. 3.2.
50 3.

car -
, cdr .
,
:
> (setf z (list a (list b c) d))
(A (B C) D)
. 3.3; car
:
> (car (cdr z))
(B C)

nil

a nil d

b c

. 3.3.

.
.
(nested), , -
, (flat).
, cons-, -
consp. listp :
(defun out-listp (x)
(or (null x) (consp x)))
atom, , -
, cons-:
(defun our-atom (x) (not (consp x)))
nil, -
, .

3.2.
, cons,
. , , cons
, , -
, :
3.3. 51

> (eql (cons a nil) (cons a nil))


NIL
eql1 t (true),
.
> (setf x (cons a nil))
(A)
> (eql x x)
T
( )
equal.
:
> (equal x (cons a nil))
T
, equal
2, , -
eql, equal:
(defun our-equal (x y)
(or (eql x y)
(and (consp x)
(consp y)
(our-equal (car x) (car y))
(our-equal (cdr x) (cdr y)))))

3.3.
, , ,

. ,
cons- car cdr.
.
.
, , . -
.
:
> (setf x (a b c))
(A B C)
> (setf y x)
(A B C)

1
eql eq. Common Lisp eq -
, -
eql. eq . 234.
2
our-equal , -
. . . .
52 3.

x=
nil
y=

a b c

. 3.4. ,

, y x? -
, x, ,
. y ,
(. 3.4).
eql:
> (eql x y)
T
, ,
, , .
- -
, , , . -
- -
, , .
, -
, , .
-
. , -
, ,
-
, . ,
.
,
, .

x= nil

a b c

y= nil

. 3.5.
3.4. 53

3.4.
copy-list .
,
cons-:
> (setf x (a b c)
y (copy-list x))
(A B C)
. 3.5.
copy-list :
(defun our-copy-list (lst)
(if (atom lst)
lst
(cons (car lst) (our-copy-list (cdr lst)))))
, x (copy-list x)
equal, eql , x nil.
append , -
:
> (append (a b) (c d) (e))
(A B C D E)
append , .

3.5. :

.
(run-length encoding). :
,
:
? .
, , -
.
, .
, .
.
, .
. -
, , .
. 3.6 . compress
:
> (compress (1 1 1 0 1 0 0 0 0 1))
((3 1) 0 1 (4 0) 1)
54 3.

(defun compress (x)


(if (consp x)
(compr (car x) 1 (cdr x))
x))
(defun compr (elt n lst)
(if (null lst)
(list (n-elts elt n))
(let ((next (car lst)))
(if (eql next elt)
(compr elt (+ n 1) (cdr lst))
(cons (n-elts elt n)
(compr next 1 (cdr lst)))))))
(defun n-elts (elt n)
(if (> n 1)
(list n elt)
elt))

. 3.6. :

- ,
, .
compr, -
: elt ; n
; lst ,
. , n-elts,
n elt. -
lst - elt, n . -

, -
compr .
, uncompress
(. 3.7):
> (uncompress ((3 1) 0 1 (4 0) 1))
(1 1 1 0 1 0 0 0 0 1)
,
list-of:
> (list-of 3 ho)
(HO HO HO)
, list-of. -
make-list , -
(keyword), .
compress uncompress, . 3.6 3.7, -
, . -
3.6. 55

,
. ,
.

(defun uncompress (lst)


(if (null lst)
nil
(let ((elt (car lst))
(rest (uncompress (cdr lst))))
(if (consp elt)
(append (apply #list-of elt)
rest)
(cons elt rest)))))
(defun list-of (n elt)
(if (zerop n)
nil
(cons elt (list-of (- n 1) elt))))

. 3.7. :


-
. ,
.
load. . 3.6 3.7
"compress.lisp", , toplevel
(load "compress.lisp")
, -
toplevel.
, -
".lsp", ".lisp".

3.6.
Common Lisp
, car cdr.
, nth:
> (nth 0 (a b c))
A
56 3.

n- , nthcdr:
> (nthcdr 2 (a b c))
(C)
nth nthcdr 0. ,
Common Lisp ,
, .
, nth car
nthcdr. nthcdr :
(defun our-nthcdr (n lst)
(if (zerop n)
lst
(our-nthcdr (- n 1) (cdr lst))))
zerop , .
last cons- :
> (last (a b c))
(C)
; , -
, car last.
Common Lisp
, -
( first tenth). , -
(second x) (nth 1 x).
, Common Lisp caddr (-
car cdr cdr). cxr,
x a d .
cadr, , -
,
.

3.7.
Common Lisp -
. -
mapcar,
:
> (mapcar #(lambda (x) (+ x 10))
(1 2 3))
(11 12 13)
> (mapcar #list
(a b c)
(1 2 3 4))
((A 1) (B 2) (C 3))
3.8. 57

, mapcar -
. -
.
maplist, -
car, cdr , .
> (maplist #(lambda (x) x)
(a b c))
((A B C) (B C) (C))
mapc,
. 102, mapcan, -
. 209.

3.8.
Cons- : car -
, cdr . , (a (b c) d)
. 3.8. ( 45
, . 3.3.)
Common Lisp -
. , copy-tree -
. :
(defun our-copy-tree (tr)
(if (atom tr)
tr
(cons (our-copy-tree (car tr))
(our-copy-tree (cdr tr)))))
copy-list (. 53). copy-list -
cdr , copy-tree car.

nil

b nil d

. 3.8.
58 3.

.
Common Lisp
, ,
. , -
, :
(and (integerp x) (zerop (mod x 2)))
x y.
substitute:
> (substitute y x (and (integerp x) (zerop (mod x 2))))
(AND (INTEGERP X) (ZEROP (MOD X 2)))
, substitute , -
, x.
subst, :
> (subst y x (and (integerp x) (zerop (mod x 2))))
(AND (INTEGERP Y) (ZEROP (MOD Y 2)))
subst copy-tree:
(defun our-subst (new old tree)
(if (eql tree old)
new
(if (atom tree)
tree
(cons (our-subst new old (car tree))
(our-subst new old (cdr tree))))))
, ,
, car cdr. -
.

3.9. ,

, , -
. ( -
, , . 291.) -
, -
-
.
,
.
.
, , ,
, . , ,
:
3.9. , 59

(defun len (lst)


(if (null lst)
0
(+ (len (cdr lst)) 1)))
, 1:
1. , 0.
2. , n,
n+1.
, -
.
: lst nil, -
0. , -
n. , n+1
, 1 cdr , n+1.
, .
,
.
, , -
, . ,
our-copy-tree (. 41) : , -
, , n+1 .
(base case).
, ,
-
, member:
(defun our-member (obj lst) ; wrong2
(if (eql (car lst) obj)
lst
(our-member obj (cdr lst))))
,
-
. -
.
-
.
, ,
. 6.9.

1
.
. .
2
; wrong .
; .
60 3.

3.10.
.
, , ,
member:
> (member b (a b c))
(B C)
, member t, ,
. , -
, member -
. member
eql.
. (keyword) -
Common Lisp . -
-
, , . -
, .
, member, :test.
eql
, equal:
> (member (a) ((a) (z)) :test #equal)
((A) (Z))
-
, .
member :key.
, :
> (member a ((a b) (c d)) :key #car)
((A B) (C D))
, car a.

:
> (member 2 ((1) (2)) :key #car :test #equal)
((2))
> (member 2 ((1) (2)) :test #equal :key #car)
((2))
member-if , -
, oddp (, ):
> (member-if #oddp (2 3 4))
(3 4)
member-if
:
3.11. 61

(defun our-member-if (fn lst)


(and (consp lst)
(if (funcall fn (car lst)
lst
(our-member-if fn (cdr lst)))))
adjoin cons. -
, (. .
member):
> (adjoin b (a b c))
(A B C)
> (adjoin z (a b c))
(Z A B C)
adjoin , member.
Common Lisp -
, , , , -
: union, intersection, set-difference.
-
, member.
> (union (a b c) (c b s))
(A C B S)
> (intersection (a b c) (b b c))
(B C)
> (set-difference (a b c d e) (b e))
(A C D)
, ,
. -
, set-difference
(d c a).

3.11.
-
, . Common
Lisp -
. -
.
4.4.
length:
> (length (a b c))
3
, -
.
62 3.

subseq.
() , -
() , -
.
> (subseq (a b c d) 1 2)
(B)
> (subseq (a b c d) 1)
(B C D)
, -
.
reverse , -
:
> (reverse (a b c))
(C B A)
reverse , , , . . -
, (-
, (a b b a)).
.
length, subseq reverse, mirror?1:
(defun mirror? (s)
(let ((len (length s)))
(and (evenp len)
(let ((mid (/ len 2)))
(equal (subseq s 0 mid)
(reverse (subseq s mid)))))))
, :
> (mirror? (a b b a))
T
Common Lisp
sort. , , -
:
> (sort (0 2 1 3 8) #>)
(8 3 2 1 0)
sort , -
. sort -
, .
, .
sort nth, , -
n n- :

1
, -
, : (equal x (reverse x)). . .
3.12. 63

(defun nthmost (n lst)


(nth (- n 1)
(sort (copy-list lst) #>)))
, nth -
, , nthmost .
> (nthmost 2 (0 2 1 3 8))
3
nthmost ,
.
every some -
. ,
, :
> (every #oddp (1 3 5))
T
> (some #evenp (1 2 3))
T
, -
, ,
:
> (every #> (1 3 5) (0 2 4))
T
, -
.

3.12.

(stack). Common Lisp
: (push x y) x -
y, (pop x) .
setf.
(push obj lst)

(setf lst (cons obj lst))

(pop lst)

(let ((x (car lst))
(setf lst (cdr lst))
x)
64 3.

,
> (setf x (b))
(B)
> (push a x)
(A B)
> x
(A B)
> (setf y x)
(A B)
> (pop x)
A
> x
(B)
> y
(A B)

. 3.9.

x=

y= nil

a b

. 3.9. push pop

push -
reverse :
(defun our-reverse (lst)
(let ((acc nil))
(dolist (elt lst)
(push elt acc))
acc))
-
, , .
, .
pushnew push, adjoin cons:
> (let ((x (a b)))
(pushnew c x)
(pushnew a x)
x)
(C A B)
a , .
3.13. 65

3.13.
, list,
(proper list).
nil, cons-, cdr . -
, ,
1:
(defun proper-list? (x)
(or (null x)
(and (consp x)
(proper-list? (cdr x)))))
, cons
, , . car
, cdr .
> (setf pair (cons a b))
(A . B)
cons- , -
car cdr .
(. 3.10).

a b

. 3.10.

,
:
> (a . (b . (c . nil)))
(A B C)
,
. 3.2 3.10.
:
> (cons a (cons b (cons c d)))
(A B C . D)
. 3.11.

1
, , . .
nil , .
, . -
12.7.
66 3.

a b c d

. 3.11.

, (a b) -
:
(a . (b . nil))
(a . (b))
(a b . nil)
(a b)
.

3.14.
cons- -
.
(assoc-list, alist). -
, :
> (setf trans ((+ . "add") (- . "subtract")))
((+ . "add") (- . "subtract"))
,
. Common Lisp assoc
:
> (assoc + trans)
(+ . "add")
> (assoc * trans)
NIL
assoc , nil.
assoc:
(defun our-assoc (key alist)
(and (consp alist)
(let ((pair (car alist)))
(if (eql key (car pair))
pair
(our-assoc key (cdr alist))))))
member, assoc
, :test :key. Common Lisp assoc-if,
member-if.
3.15. : 67

3.15. :
. 3.12 ,
( ). shortest-path -
, ,
, .
, -
( . ).
, . 3.13, :
(setf min ((a b c) (b c) (c d)))
, a, assoc:
> (cdr (assoc a min))
(B C)

(defun shortest-path (start end net)


(bfs end (list (list start)) net))
(defun bfs (end queue net)
(if (null queue)
nil
(let ((path (car queue)))
(let ((node (car path)))
(if (eql node end)
(reverse path)
(bfs end
(append (cdr queue)
(new-paths path node net))
net))))))
(defun new-paths (path node net)
(mapcar #(lambda (n)
(cons n path))
(cdr (assoc node net))))

. 3.12.

a d

. 3.13.
68 3.

. 3.12 (breadth-first search).


, -
. -
.
. 3.12 ,
, ,
. , -
, .
bfs.
. , shortest-path
bfs (list (list start)) .
, bfs, , -
. , bfs nil, ,
. , bfs -
. car ,
, , ,
. -
. bfs -
.
bfs ,
,
:
> (shortest-path a d min)
(A C D)
-
bfs:
((A))
((B A) (C A))
((C A) (C B A))
((C B A) (D C A))
((D C A) (D C B A))
-
, (cdr) -
.
, . 3.12 -
. -
.
: , -
1 , -
.

1
12.3 , -
.
3.16. 69

3.16.
-
. -
,
, -
, .
,
, -
-
. , -
.
-
. - , -
(heap). -

. , cons
, consing.
, , -
, . -
, -
. , -
, .
(garbge collection, GC).
? :
> (setf lst (list a b c))
(A B C)
> (setf lst nil)
NIL
list, cons, ,
, cons- . -
lst . ,
cons, 1.
, -
, . -
, cons, .
-
, -
. , -

1
. toplevel *, **, ***, -
.
,
.
70 3.

.
.
, . -
. -
consing.
, , ,
, cons-
. , consing
-
. , ,

, consing -
, .
, , -
. , remove , -
, .
, ,
, .
12.4.
, -
. -
-
,
.
,
-
, ,
. -
13.4.
, consing .
, , -
, , -
, .


1. Cons- , .
.
2. equal , eql. -
, .
3. . -
.
4. copy-list, -
append.
71

5. ,
.
6. Common Lisp
, car cdr.
7. -
.
8.
.
9. , -
, .
10. . -
.
11.
, .
12. . Common Lisp -
.
13. Cons-, , -
.
14. -
. .
15.
,
.

1. :
(a) (a b (c d))
(b) (a (b (c (d))))
(c) (((a b) c) d)
(d) (a (b . c) . d)
2. union,
:
> (new-union (a b c) (b a d))
(A B C D)
3. , ( -
eql)
:
> (occurrences (a b a d a c d c a))
((A . 4) (C . 2) (D . 2) (B . 1))
4. (member (a) ((a) (b))) nil?
72 3.

5. pos+ , -

:
> (pos+ (7 5 1 4))
(7 6 3 7)
: (a) , (b)
(c) mapcar.
6. -
, cdr
, car . , -
:
(a) cons
(b) list1
(c) length ( )
(d) member ( , )
7. . 3.6 ,
. (: .)
8. , -
:
> (showdots (a b c))
(A . (B . (C . NIL)))
NIL
9. , ,
( 3.15). -
.

1
6b -
. (&rest),
. 114. . .
.
4
4.

-
.
: (
), -. , -
,
.
Common Lisp
(instance). 11, CLOS.

4.1.
Common Lisp make-array, -
.
23:
> (setf arr (make-array (2 3) :initial-element nil))
#<Simple-Array T (2 3) BFC4FE>
Common Lisp
7 , -
1 023 1.
:initial-element . -
, .
,
, .

1

. . .
74 4.

, aref. -
Common Lisp, aref -
:
> (aref arr 0 0)
NIL
, setf -
aref:
> (setf (aref arr 0 0) b)
B
> (aref arr 0 0)
B
, -
#na, n . , -
arr :
#2a((b nil nil) (nil nil nil))
*print-array*1 t, -
:
> (setf *print-array* t)
T
> arr
#2A((B NIL NIL) (NIL NIL NIL))

make-array :
> (setf vec (make-array 4 :initial-element nil))
#(NIL NIL NIL NIL)
.
vector:
> (vector "a" b 3)
#("a" B 3)
, -
#na, #().
aref,
svref:
> (svref vec 0)
NIL

1
*print-array* t.
. .
4.2. : 75

sv simple vector.
.1

4.2. :
, -
. , -
,
, find
(. 80). -
, .
, . -
, -
.
. 4.1 , .
: bin-search2
finder,
start end vec.

(defun bin-search (obj vec)


(let ((len (length vec)))
(and (not (zerop len))
(finder obj vec 0 (- len 1)))))
(defun finder (obj vec start end)
(let ((range (- end start)))
(if (zerop range)
(if (eql obj (aref vec start))
obj
nil)
(let ((mid (+ start (round (/ range 2)))))
(let ((obj2 (aref vec mid)))
(if (< obj obj2)
(finder obj vec start (- mid 1))
(if (> obj obj2)
(finder obj vec (+ mid 1) end)
obj)))))))

. 4.1.

1
(adjustable), -
(displaced) (fill-pointer). -
. -
, .
2
bin-search , , -
.
. . .
76 4.

,
obj, -
nil. -
, obj2 ( round
),
obj. obj obj2, -
, . -
obj = obj2, ,
.
finder,
(format t "~A~%" (subseq vec start (+ end 1)))
:
> (bin-search 3 #(0 1 2 3 4 5 6 7 8 9))
#(0 1 2 3 4 5 6 7 8 9)
#(0 1 2 3)
#(3)
3


Common Lisp , ,
. -
,
: , -
,
, , . -
,
. 4.1 :
;;;;

;;;

(defun bin-search (obj vec)


(let ((len (length vec)))
;; , finder
(and (not (zerop len)) ; nil,
(finder obj vec 0 (- len 1)))))

#|...|#. , #| |#, -
.
4.3. 77

4.3. 1
, .
, . , -
c, : #\c.
, ,
( ) ASCII. -
char-code,
, code-char, .
: char< (-
), char<= ( ), char= (), char>= ( ),
char> () char/= ( ). ,
, . 157.
> (sort "elbow" #char<)
"below"
,
. , , -
, aref:
> (aref "abc" 1)
#\b
-
char:
> (char "abc" 1)
#\b
char, aref, setf
:
> (let ((str (copy-seq "Merlin")))
(setf (char str 3) #\k)
str)
"Merkin"
, -
equal, string-equal,
:
> (equal "fred" "fred")
T
> (equal "fred" "Fred")
NIL

1

-, (, -
), .
,
. . .
78 4.

> (string-equal "fred" "Fred")


T
Common Lisp -
. D, -
. 378.
.
format. nil
format , :
> (format nil "~A or ~A" "truth" "dare")
"truth or dare"
, -
concatenate, -
:
> (concatenate string "not " "to worry")
"not to worry"

4.4.
(sequence) Common Lisp -
( , ). ,
,
. , , remove, length, subseq, reverse, sort,
every, some. , , . 62, -
:
> (mirror? "abba")
T
-
: nth , aref svref , char .
-
elt:
> (elt (a b c) 1)
B
, elt
,
.
elt mirror? -
:
(defun mirror? (s)
(let ((len (length s)))
(and (evenp len)
(do ((forward 0 (+ forward 1))
(back (- len 1) (- back 1)))
((or (> forward back)
4.4. 79

(not (eql (elt s forward)


(elt s back))))
(> forward back))))))
- ,
. -
, -
.
.
, , -
:
-

:key , identity
:test eql
:from-end t, nil
:start , 0
:end , , nil

, , position.
-
nil .
position:
> (position #\a "fantasia")
1
> (position #\a "fantasia" :start 3 :end 5)
4
-
. :start ,
:end , -
.
:from-end:
> (position #\a "fantasia" :from-end t)
7
, -
. ,
( ).
:key , -
:
> (position a ((c d) (a b)) :key #car)
1
, car a.
80 4.

:test , -
. eql. -
, equal:
> (position (a b) ((a b) (c d)))
NIL
> (position (a b) ((a b) (c d)) :test #equal)
0
:test . -
, < , :
> (position 3 (1 0 7 5) :test #<)
2
subseq position
. ,
(defun second-word (str)
(let ((p1 (+ (position #\ str) 1)))
(subseq str p1 (position #\ str :start p1))))
:
> (second-word "Form follows function.")
"follows"
, , -
position-if. -
, , -
:
> (position-if #oddp (2 3 4 5))
1
,
:test.
,
member member-if. find ( ) find-if
( , :test):
> (find #\a "cat")
#\a
> (find-if #characterp "ham")
#\h
member member-if,
.
find-if find :key. -
, :
(find-if #(lambda (x)
(eql (car x) complete))
lst)
4.5. : 81

:
(find complete lst :key #car)
remove (. 39) remove-if
. , find
find-if. remove-duplicates -
, :
> (remove-duplicates "abracadabra")
"cdbra"
, -
.
reduce . -
, , -
.
,
. -
reduce. -
, :
(reduce #fn (a b c d))

(fn (fn (fn a b) c) d)
reduce -
, . , -
, :
> (reduce #intersection ((b r a d s) (b a d) (c a t)))
(A)

4.5. :

. , -
"16 Aug 1980" , -
, .
. 4.2 , -
. , tokens, .
tokens , -
, .
. alpha-char-p , -
. :
> (tokens "ab12 3cde.f" #alpha-char-p 0)
("ab" "cde" "f")
, , -
.
82 4.

(defun tokens (str test start)


(let ((p1 (position-if test str :start start)))
(if p1
(let ((p2 (position-if #(lambda (c)
(not (funcall test c)))
str :start p1)))
(cons (subseq str p1 p2)
(if p2
(tokens str test p2)
nil)))
nil)))
(defun constituent (c)
(and (graphic-char-p c)
(not (char= c #\ ))))

. 4.2.

constituent
tokens. Common Lisp (graphic characters) -
, , . tokens
constituent , -
:
> (tokens "ab12 3cde.f
gh" #constituent 0)
("ab12" "3cde.f" "gh")
. 4.3 , .

(defun parse-date (str)


(let ((toks (tokens str #constituent 0)))
(list (parse-integer (first toks))
(parse-month (second toks))
(parse-integer (third toks)))))
(defconstant month-names
#("jan" "feb" "mar" "apr" "may" "jun"
"jul" "aug" "sep" "oct" "nov" "dec"))
(defun parse-month (str)
(let ((p (position str month-names
:test #string-equal)))
(if p
(+ p 1)
nil)))

. 4.3.
4.6. 83

parse-date , ,
, :
> (parse-date "16 Aug 1980")
(16 8 1980)
parse-month parse-
integer . parse-month
, string-equal. -
, , ,
parse-integer.
Common Lisp ,
:
(defun read-integer (str)
(if (every #digit-char-p str)
(let ((accum 0))
(dotimes (pos (length str))
(setf accum (+ (* accum 10)
(digit-char-p (char str pos)))))
accum)
nil))
read-integer , Common
Lisp .
digit-char-p, , -
, , .

4.6.

. , , -
. -
, : ,
. , -
svref :
(defun block-height (b) (svref b 0))
. ,
.
defstruct. -
:
(defstruct point
x
y)
point, , x y. ,
: make-point, point-p, copy-point, point-x,
point-y.
84 4.

2.3 - -
-. :
defstruct . -
, -
. ( defstruct,
.)
make-point -
point.
:
> (setf p (make-point :x 0 :y 0))
#S(POINT X 0 Y 0)

, setf:
> (point-x p)
0
> (setf (point-y p) 2)
2
> p
#S(POINT X 0 Y 2)

. point point, structure,
atom t. , point-p -
:
> (point-p p)
T
> (typep p point)
T
typep .
,

.
(defstruct polemic
(type (progn
(format t "What kind of polemic was it? ")
(read)))
(effect nil))
make-polemic -
:
> (make-polemic)
What kind of polemic was it? scathing
#S(POLEMIC TYPE SCATHING EFFECT NIL)
4.7. : 85

, ,
.
point:
(defstruct (point (:conc-name p)
(:print-function print-point))
(x 0)
(y 0))
(defun print-point (p stream depth)
(format stream "#<~A, ~A>" (px p) (py p)))
:conc-name , -
.
point-, p. -
,
,
.
:print-function ,
, (, top-
level). : ;
, ;
1. - -
7.1. ,
, , format.
print-point
:
> (make-point)
#<0,0>

4.7. :
Common Lisp sort, , -
, .
, , -
: .

(BST). BST ,
, log n, n -
.
BST , -
( <)
: < -, >

1
ANSI Common Lisp :print-object -
. , print-unreadable-object,
#<...>.
86 4.

. . 4.4 BST, -
<.

4 8

2 6 9

1 3 7

. 4.4.

. 4.5 -
BST. .
: , -
. cons-
car cdr.
BST nil, , (l r)
BST. .
cons, -
bst-insert.
, -
.
> (setf nums nil)
NIL
> (dolist (x (5 8 4 2 1 9 6 7 3))
(setf nums (bst-insert x nums #<)))
NIL
nums . 4.4.
bst-find, , -
, bst-insert. ,
bst-find our-member (. 33).
member, bst-find , :
> (bst-find 12 nums #<)
NIL
> (bst-find 4 nums #<)
#<4>
, -
(nil) nil.
4.7. : 87

(defstruct (node (:print-function


(lambda (n s d)
(format s "#<~A>" (node-elt n)))))
elt (l nil) (r nil))
(defun bst-insert (obj bst <)
(if (null bst)
(make-node :elt obj)
(let ((elt (node-elt bst)))
(if (eql obj elt)
bst
(if (funcall < obj elt)
(make-node
:elt elt
:l (bst-insert obj (node-l bst) <)
:r (node-r bst))
(make-node
:elt elt
:r (bst-insert obj (node-r bst) <)
:l (node-l bst)))))))
(defun bst-find (obj bst <)
(if (null bst)
nil
(let ((elt (node-elt bst)))
(if (eql obj elt)
bst
(if (funcall < obj elt)
(bst-find obj (node-l bst) <)
(bst-find obj (node-r bst) <))))))
(defun bst-min (bst)
(and bst
(or (bst-min (node-l bst)) bst)))
(defun bst-max (bst)
(and bst
(or (bst-max (node-r bst)) bst)))

. 4.5. :

BST -
. ,
, (bst-min). ,
, (bst-max):
> (bst-min nums)
#<1>
> (bst-max nums)
#<9>
88 4.

,
(. 4.6) .

(defun bst-remove (obj bst <)


(if (null bst)
nil
(let ((elt (node-elt bst)))
(if (eql obj elt)
(percolate bst)
(if (funcall < obj elt)
(make-node
:elt elt
:l (bst-remove obj (node-l bst) <)
:r (node-r bst))
(make-node
:elt elt
:r (bst-remove obj (node-r bst) <)
:l (node-l bst)))))))
(defun percolate (bst)
(let ((l (node-l bst)) (r (node-r bst)))
(cond ((null l) r)
((null r) l)
(t (if (zerop (random 2))
(make-node :elt (node-elt (bst-max l))
:r r
:l (bst-remove-max l))
(make-node :elt (node-elt (bst-min r))
:r (bst-remove-min r)
:l l))))))
(defun bst-remove-min (bst)
(if (null (node-l bst))
(node-r bst)
(make-node :elt (node-elt bst)
:l (bst-remove-min (node-l bst))
:r (node-r bst))))
(defun bst-remove-max (bst)
(if (null (node-r bst))
(node-l bst)
(make-node :elt (node-elt bst)
:l (node-l bst)
:r (bst-remove-max (node-r bst)))))

. 4.6. :
4.7. : 89

bst-remove1 , -
. remove,
bst-remove :
> (setf nums (bst-remove 2 nums #<))
#<5>
> (bst-find 2 nums #<)
NIL
nums . 4.7. (
1 2.)

4 8

3 6 9

1 7

. 4.7.

, -
, -
. percolate.
,
. .
, percolate
. (random 2) 0, 1,
(zerop (random 2)) .
, , -
.

1
bst-remove, , .
: (left child) < node < (right child) -
. : max(left sub-
tree) < node < min(right subtree). bst-traverse,
. 4.8, .
(: 1 2,
0.) , bst-insert,
. 4.5, . , BST
bst-remove . -
, -
, .
. 4.6 . . .
90 4.

(defun bst-traverse (fn bst)


(when bst
(bst-traverse fn (node-l bst))
(funcall fn (node-elt bst))
(bst-traverse fn (node-r bst))))

. 4.8. :

bst-traverse (. 4.8), -
fn.
> (bst-traverse #princ nums)
13456789
NIL
( princ .)
, ,
. , - -
. ,
elt,
.
, -
.
-
. ,
.
, , , , BST .
,
.
, O(log n) -
O(n). , -
, BST -
, .

4.8. -
3 , -
.
( 10 ) - -
. -
make-hash-table,
:
> (setf ht (make-hash-table))
#<Hash-Table BF0A96>
-, , #<...>.
4.8. - 91

-, , -
. ,
, gethash . -
gethash nil, .
> (gethash color ht)
NIL
NIL
Common Lisp:
. gethash
. . -
, nil ( ), , -
. nil?
, , color,
nil, gethash , t.

, -
, . 5.5
, .
- , setf
gethash:
> (setf (gethash color ht) red)
RED
gethash :
> (gethash color ht)
RED
T
, gethash
, .
, -, . -
,
, , -
:
> (setf bugs (make-hash-table))
#<Hash-Table BF4C36>
> (push "Doesnt take keyword arguments. "
(gethash #our-member bugs))
("Doesnt take keyword arguments. ")
gethash nil, push
setf, . (
our-member . 39.)
- -
. -
.
92 4.

, -, setf
gethash:
> (setf fruit (make-hash-table))
#<Hash-Table BFDE76>
> (setf (gethash apricot fruit) t)
T
-
gethash:
> (gethash apricot fruit)
T
T
gethash nil, -
.
, remhash:
> (remhash apricot fruit)
T
t, remhash ,
.
- maphash,
.
- -
.
> (setf (gethash shape ht) spherical
(gethash size ht) giant)
GIANT
> (maphash #(lambda (k v)
(format t "~A = ~A~%" k v))
ht)
SHAPE = SPHERICAL
SIZE = GIANT
COLOR = RED
NIL
maphash nil,
, , , , -
.
- ,
,
. -
:size make-hash-table. -
, :
( -
),
. :
93

:size , -
-. :
(make-hash-table :size 5)
-, ,
.
, -
, -
. eql,
eq, equal equalp;
:test:
> (setf writers (make-hash-table :test #equal)
#<Hash-Table C005E6>
> (setf (gethash (ralph waldo emerson) writers) t)
T
,
-. , -
member,
. - -
, -
.
-
( ) . -
: -
, -
.


1. Common Lisp .
.
2. . -
.
3. . -

.
4. Common Lisp -
.
5. defstruct .
, .
6.
.
7. -
.
94 4.

1. , (
(n n)) 90 :
> (quarter-turn #2A((a b) (c d)))
#2A((C A) (D B))
array-dimensions (. 374).
2. reduce . 382, -
:
() copy-list
(b) reverse ( )
3. , -
. :
(a) , ( -

eql);
(b) , -
, ( eql)
.
4. , BST- -
, .
5. bst-adjoin1. bst-insert, -
, -
.
6. -
(k . v) -
. , :
(a) - ;
(b) -.

1
, bst-adjoin -
bst-insert. . . .
5
5.

2.2 , -
. -
, ,
.
-,
.

5.1.
Common Lisp :
progn, block tagbody. . , -
progn, ,
:
> (progn
(format t "a")
(format t "b")
(+ 11 12))
ab
23
, -
progn ( ) -
.
block progn . -
, . -
,
return-from :
> (block head
(format t "Here we go.")
96 5.

(return-from head idea)


(format t "Well never see this. "))
Here we go.
IDEA
return-from ,
. return-from -
, .
, return-
from, .
, return, -
nil:
> (block nil
(return 27))
27
Common Lisp, , -
block nil. ,
:
> (dolist (x (a b c d e))
(format t "~A " x)
(if (eql x c)
(return done)))
A B C
DONE
, defun, ,
:
(defun foo ()
(return-from foo 27))
block return, return-from .
return-from
read-integer:
(defun read-integer (str)
(let ((accum 0))
(dotimes (pos (length str))
(let ((i (digit-char-p (char str pos))))
(if i
(setf accum (+ (* accum 10) i))
(return-from read-integer nil))))
accum))
read-integer (. 83) -
. ,
return-from , -
.
, tagbody,
go. , , -
5.2. 97

, .
1 10:
> (tagbody
(setf x 0)
top
(setf x (+ x 1))
(format t "~A " x)
(if (< x 10) (go top)))
1 2 3 4 5 6 7 8 9 10
NIL
tagbody ,
, , ,
. -
tagbody, ( ) -
go.
, ? -
progn.
block. tagbody .

5.2.
let, , -
.
, :
> (let ((x 7)
(y 2))
(format t "Number")
(+ x y))
Number
9
let (lexical
context). -
, .
let . 2.14 -
, , -
-. - ,
,
:
> ((lambda (x) (+ x 1)) 3)
4
let, ,
-:
((lambda (x y)
(format t "Number")
98 5.

(+ x y))
7
2)
, let, -
, lambda.
, . -
let, -
let-:
(let ((x 2)
(y (+ x 1)))
(+ x y))
x (+ x 1) ,
-:
((lambda (x y) (+ x y)) 2
(+ x 1))
, (+ x 1) -
x -.
, , let- -
? let*:
> (let* ((x 1)
(y (+ x 1)))
(+ x y))
3
let* let.
:
(let ((x 1))
(let ((y (+ x 1)))
(+ x y)))
let, let* nil. -
, :
> (let (x y)
(list x y))
(NIL NIL)
destructing-bind let.
(pattern) -
, ,
. :
> (destructing-bind (w (x y) . z) (a (b c) d e)
(list w x y z))
(A B C (D E))
, -
, .
5.3. 99

5.3.
if. -
. when
if. , , -
. ,
(when (oddp that)
(format t "Hmm, thats odd. ")
(+ that 1))

(if (oddp that)
(progn
(format t "Hmm, thats odd. ")
(+ that 1)))
unless when.
.
( ) cond, -
: -
progn .
, if -
. , our-member:
(defun our-member (obj lst)
(if (atom lst)
nil
(if (eql (car lst) obj)
lst
(our-member obj (cdr lst)))))
cond:
(defun our-member (obj lst)
(cond ((atom lst) nil)
((eql (car lst) obj) lst)
(t (our-member obj (cdr lst)))))
, Common Lisp, , -
.
cond . -
, -
, -
. cond
, -
. , ,
. ,
, ,
:
100 5.

> (cond (99))


99
t, .
,
, .
nil, -
. ( . 132.)
, -
, case. -
, case -
:
(defun month-length (mon)
(case mon
((jan mar may jul aug oct dec) 31)
((apr jun sept nov) 30)
(feb (if (leap-year) 29 28))
(otherwise "unknown month")))
case ,
. -
, , ,
. -
. ()
eql. -
.
case .
t otherwise. -
, -
, , nil:
> (case 99 (99))
NIL
typecase case, -
, typep -
eql. ( typecase . 118.)

5.4.
, do, 2.13.
do block tagbody,
return, return-from go.
2.13 , do -
,

(variable initial update)


5.4. 101

initial update . update


, .
initial,
nil.
. 40:
(defun show-squares (start end)
(do ((i start (+ i 1)))
((> i end) done)
(format t "~A ~A~%" i (* i i))))
, do, update. -
do
.
:
, -
, ?
: -
? do -
:
> (let ((x a))
(do ((x 1 (+ x 1))
(y x x))
((> x 5))
(format t "(~A ~A) " x y)))
(1 A) (2 1) (3 2) (4 3) (5 4)
NIL
x 1, y -
x .
do*, do, let* let.
(initial) (update) -
,
:
> (do* ((x 1 (+ x 1))
(y x x))
((> x 5))
(format t "(~A ~A) " x y))
(1 1) (2 2) (3 3) (4 4) (5 5)
NIL
do do* .
dolist:
> (dolist (x (a b c d) done)
(format t "~A " x))
A B C D
DONE
102 5.

do
The Evolution of Lisp
do , -
:
. ,
, , -
. -

,
. -
,

. do -
, -
:
(defun factorial (n)
(do ((j n (- j 1))
(f 1 (* j f)))
((= j 0) f)))
, do ,
.


. nil.
dotimes,
n 0 n1:
> (dotimes (x 5 x)
(format t "~A " x))
0 1 2 3 4
5
dolist,
nil. , -
.
mapc mapcar, -
,
. , dolist,
:
> (mapc #(lambda (x y)
(format t "~A ~A " x y))
(hip flip slip)
(hop flop slop))
5.5. 103

HIP HOP FLIP FLOP SLIP SLOP


(HIP FLIP SLIP)
mapc .

5.5.
,
, .
, . Common Lisp
. -
,
19.
, ,
- . -
, get-decoded-time -
: , , , , ,
.
-
, nil. -
gethash . -
, nil -
, .
va-
lues. ,
:
> (values a nil (+ 2 4))
A
NIL
6
values ,
, values. -
:
> ((lambda () ((lambda () (values 1 2)))))
1
2
-
, :
> (let ((x (values 1 2)))
x)
1
values ,
. - ,
nil.
104 5.

> (values)
> (let ((x (values)))
x)
NIL
, multiple-
value-bind:
> (multiple-value-bind (x y z) (values 1 2 3)
(list x y z))
(1 2 3)
> (multiple-value-bind (x y z) (values 1 2)
(list x y z))
(1 2 NIL)
, , -
nil.
, , -
. , , -
:
> (multiple-value-bind (s m h) (get-decoded-time)
(format nil "~A:~A:~A" h m s))
"4:32:13"
- -
multiple-value-call:
> (multiple-value-call #+ (values 1 2 3))
6
, multiple-value-list:
> (multiple-value-list (values a b c))
(A B C)
, multiple-value-call #list
.

5.6.
, return. -
, -
.
catch throw. catch
, . -
.
(defun super ()
(catch abort
(sub)
(format t "Well never see this.")))
5.6. 105

(defun sub ()
(throw abort 99))
, progn. throw

catch:
> (super)
99
throw -
catch ( )
catch ( ).
catch , throw .
error , -
-
. , , (break loop).
Common Lisp:
> (progn
(error "Oops! ")
(format t "After the error. "))
Error: Oops!
Options: :abort, :backtrace
>>

A.

throw. unwind-protect, -
, -
. unwind-protect
. -
, .
> (setf x 1)
1
> (catch abort
(unwind-protect
(throw abort 99)
(setf x 2)))
99
> x
2
, throw catch,
,
catch. -
- , unwind-protect -
. . 295.
106 5.

5.7. :

, , , 60 17 -
1997 15 1998.
.
, 1 2000 -
. , -
+ . ,
.
,
, . , -
, 13 2004 , , -
2004 , 13.
, -
. -
, :
> (setf mon (31 28 31 30 31 30 31 31 30 31 30 31))
(31 28 31 30 31 30 31 31 30 31 30 31)
, :
> (apply #+ mon)
365
maplist
+ (cdr) .
, :
> (setf nom (reverse mon))
(31 30 31 30 31 31 30 31 30 31 28 31)
> (setf sums (maplist #(lambda (x)
(apply #+ x))
nom))
(365 334 304 273 243 212 181 151 90 59 31)
> (reverse sums)
(31 59 90 120 151 181 212 243 273 304 334 365)
, 31 ,
59 . .
. 5.1. , . -
.
- :
, , -
. , -
.
-
, ( 10.2)
5.7. : 107

( 14.3). , month, -
, .

.
,
.

(defconstant month
#(0 31 59 90 120 151 181 212 243 273 304 334 365))
(defconstant yzero 2000)
(defun leap? (y)
(and (zerop (mod y 4))
(or (zerop (mod y 400))
(not (zerop (mod y 100))))))
(defun date->num (d m y)
(+ (- d 1) (month-num m y) (year-num y)))
(defun month-num (m y)
(+ (svref month ( - m 1))
(if (and (> m 2) (leap? y)) 1 0)))
(defun year-num (y)
(let ((d 0))
(if (>= y yzero)
(dotimes (i (- y yzero) d)
(incf d (year-days (+ yzero i))))
(dotimes (i (- yzero y) (- d))
(incf d (year-days (+ y i)))))))
(defun year-days (y) (if (leap? y) 366 365))

. 5.1. :

-
, , -
. -
. -
1752 -
, 2 14 .
, .
, 4 100
400. 1904 , 1900 , 1600 .
mod,
:
> (mod 23 5)
3
108 5.

> (mod 25 5)
0
,
. leap? -
:
> (mapcar #leap? (1904 1900 1600))
(T NIL T)
date->num.
. -
, , -
month-num, -
month, 1, -
.
, date->num
year-num, 1 -
. (
2000).
. 5.2 . num->date
. num-year, -
, num-month
.
year-num, num-year 2000 , -
,
n ( ). , -
, , num-year , -
. prev,
, .
num-month nmon month-num.
month , -
month-num
.
. 5.2 .
num-year -
num-month.
,
.
date->num num->date . -
date+,
.
60 17 1997 :
> (multiple-value-list (date+ 17 12 1997 60))
(15 2 1998)
15 1998 .
109

(defun num->date (n)


(multiple-value-bind (y left) (num-year n)
(multiple-value-bind (m d) (num-month left y)
(values d m y))))
(defun num-year (n)
(if (< n 0)
(do* ((y (- yzero 1) (- y 1))
(d (- (year-days y)) (- d (year-days y))))
((<= d n) (values y (- n d))))
(do* ((y yzero (+ y 1))
(prev 0 d)
(d (year-days y) (+ d (year-days y))))
((> d n) (values y (- n prev))))))
(defun num-month (n y)
(if (leap? y)
(cond ((= n 59) (values 2 29))
((> n 59) (nmon (- n 1)))
(t (nmon n)))
(nmon n)))
(defun nmon (n)
(let ((m (position n month :test #<)))
(values m (+ 1 (- n (svref month (- m 1)))))))
(defun date+ (d m y n)
(num->date (+ (date->num d m y) n)))

. 5.2. :


1. Common Lisp : progn,
block (return) tagbody,
goto.
.
2. -
.
3. Common Lisp ,
. -
if.
4. Common Lisp .
5. .
6. ,
.
110 5.

1. let let*,
:
(a) (let ((x (car y)))
(cons x x))
(b) (let* ((w (car x))
(y (+ w z)))
(cons w y))
2. mystery (. 46) cond.
3. , ,
,
.
4. month-num (. 5.1), case svref.
5. ( ) -
x v, , -
x v:
> (precedes #\a "abracadabra")
(#\c #\d #\r)
6. ( ), -
,
:
> (intersperse - (a b c d))
(A - B - C - D)
7. , -
,
1. :
(a)
(b) do
(c) mapc return
8. ,
.
9. . 3.12 -
.
.
(a) catch throw, ,

, .
(b) , -
catch throw.
6
6.

. -
, . -
,
.

6.1.
fboundp , , -
. - -
, symbol-function:
> (fboundp +)
T
> (symbol-function +)
#<Compiled-Function + 17BA4E>
setf symbol-function:
> (setf (symbol-function add2)
#(lambda (x) (+ x 2)))
, -
, defun:
> (add2 1)
3
defun

(defun add2 (x) (+ x 2))
setf. defun -
, -
112 6.

, , , defun -
.
defun (setf f), -
, , setf f.
primo, car:
(defun primo (lst) (car lst))

(defun (setf primo) (val lst)


(setf (car lst) val))
-
(setf primo), -
, primo.
setf primo , -
:
> (let ((x (list a b c)))
(setf (primo x) 480)
x)
(480 B C)
primo, -
(setf primo),
.
, -
. -
, , -
. ,
:
(defun foo (x)
"Implements an enhanced paradigm of diversity."
x)
, , -
documentation:
> (documentation foo function)
"Implements an enhanced paradigm of diversity."

6.2.
, defun setf symbol-func-
tion, . ,
. , -
, , ,
.

labels let .
6.3. 113

, , -
. :
(name parameters . body)
labels name -
(lambda parameters . body).
> (labels ((add10 (x) + x 10))
(consa (x) (cons a x)))
(consa (add10 3)))
(A . 13)
let, , . -
labels ,
labels, .
labels :
> (labels ((len (lst)
(if (null lst)
0
(+ (len (cdr lst)) 1))))
(len (a b c)))
3
5.2 , let -
. do -
. :
(do ((x a (b x))
(y c (d y)))
((test x y) (z x y))
(f x y))

(labels ((rec (x y)
(cond ((test x y)
(z x y))
(t
(f x y)
(rec (b x) (d y))))))
(rec a c))
,
do.

6.3.
2.1 , + -
. -
. -
, -
(rest).
114 6.

&rest
,
. -
funcall apply:
(defun our-funcall (fn &rest args)
(apply fn args))
,
. -
(optional). ( -
(required).) &optional -

(defun philosoph (thing &optional property)
(list thing is property))
,
nil:
> (philosoph death)
(DEATH IS NIL)
, -
. philosoph
(defun philosoph (thing &optional (property fun))
(list thing is property))
:
> (philosoph death)
(DEATH IS FUN)

, -.
, .
(keyword) ,
. &key ,
.
, -
,
:
> (defun keylist (a &key x y z)
(list a x y z))
KEYLIST
> (keylist 1 :y 2)
(1 NIL 2 NIL)
> (keylist 1 :y 3 :x 2)
(1 2 3 NIL)
, , -
nil, -
.
6.4. : 115

-
&rest ,
. , adjoin :
(defun our-adjoin (obj lst &rest args)
(if (apply #member obj lst args)
lst
(cons obj lst)))
adjoin , member, -
member.
5.2 destructing-bind.
, ,
, :
> (destructing-bind ((&key w x) &rest y) ((:w 3) a)
(list w x y))
(3 NIL (A))

6.4. :
2.6 , -
, , .
, -
,
. Common
Lisp - , -
, , + eql.
- -,
-. ,
, .
, -
.
, , -
. -, , -
, ,
.
-
.
- -
, -
, .
( -
), , ,
.
-,
-,
116 6.

-. , -
.
, , sort. ,
, -
sort
. .
, -
. . 6.1 .
, single? append1, ,
. -
, :
> (single? (a))
T

(defun single? (lst)


(and (consp lst) (null (cdr lst))))
(defun append1 (lst obj)
(append lst (list obj)))
(defun map-int (fn n)
(let ((acc nil))
(dotimes (i n)
(push (funcall fn i) acc))
(nreverse acc)))
(defun filter (fn lst)
(let ((acc nil))
(dolist (x lst)
(let ((val (funcall fn x)))
(if val (push val acc))))
(nreverse acc)))
(defun most (fn lst)
(if (null lst)
(values nil nil)
(let* ((wins (car lst))
(max (funcall fn wins)))
(dolist (obj (cdr lst))
(let ((score (funcall fn obj)))
(when (> score max)
(setf wins obj
max score))))
(values wins max))))

. 6.1.
6.4. : 117

cons, , cons, -
, :
> (append1 (a b c) d)
(A B C D)
, map-int, n, -
,
0 n1.
. (
, -

.) 0 9,
:
> (map-int #identity 10)
(0 1 2 3 4 5 6 7 8 9)
0
99 (), -
:
> (map-int #(lambda (x) (random 100))
10)
(85 40 73 64 28 21 67 5 32)
map-int - -
. acc,
nil, -
push. acc.1
filter. filter -

,
, nil:
> (filter #(lambda (x)
(and (evenp x) (+ x 10)))
(1 2 3 4 5 6 7))
(12 14 16)
filter remove-if.
. 6.1, most, , -
-
.
.
> (most #length ((a b) (a b c) (a)))
(A B C)
3

1
nreverse (. 229) , reverse,
.
118 6.

, most .
, . 6.1
. -

-.
, ,
-.
, , -
. -
. -
, , -
.
.

6.5.
, , -
. ,
, ,
:
(defun combiner (x)
(typecase x
(number #+)
(list #append)
(t #list)))

:
(defun combine (&rest args)
(apply (combiner (car args))
args))

. ( ,
.)
> (combine 2 3)
5
> (combine (a b) (c d))
(A B C D)
2.10 , -
, .
, -
, - .
-
, , -
,
6.5. 119

. , 3 -
:
> (setf fn (let ((i 3))
#(lambda (x) (+ x i))))
#<Interpreted-Function C0A51E>
> (funcall fn 2)
5
, -
. , -
, (clo-
sure)1. , -
.
. -
, - -
, . -
, , , , :
(defun add-to-list (num lst)
(mapcar #(lambda (x)
(+ x num))
lst))
, -
-
. num - , ,
mapcar .
, -
. ,
:
(defun make-adder (n)
#(lambda (x)
(+ x n)))
n ,
n :
> (setf add3 (make-adder 3))
#<Interpreted-Function C0EBF6>
> (funcall add3 2)
5
> (setf add27 (make-adder 27))
#<Interpreted-Function C0EE4E>
> (funcall add27 2)
29

1
.
, -
.
120 6.

, -
. ,
counter:
(let ((counter 0))
(defun reset ()
(setf counter 0))
(defun stamp ()
(setf counter (+ counter 1))))
-
. stamp , ,
. reset, :
> (list (stamp) (stamp) (reset) (stamp))
(1 2 0 1)
,
, .
Common Lisp complement, -
. :
> (mapcar (complement #oddp)
(1 2 3 4 5 6))
(NIL T NIL T NIL T)
:
(defun our-complement (f)
#(lambda (&rest args)
(not (apply f args))))
, ,
, .
. -
, .

6.6. :
Dylan Scheme Common Lisp,
Pascal. , -
. complement,
, Dylan compose, disjoin, conjoin, curry, rcurry always.
. 6.2 Common Lisp,
. 6.3 , .
, compose, -
, . -
,
(compose #a #b #c)
6.6. : 121

(defun compose (&rest fns)


(destructuring-bind (fn1 . rest) (reverse fns)
#(lambda (&rest args)
(reduce #(lambda (v f) (funcall f v))
rest
:initial-value (apply fn1 args)))))
(defun disjoin (fn &rest fns)
(if (null fns)
fn
(let ((disj (apply #disjoin fns)))
#(lambda (&rest args)
(or (apply fn args) (apply disj args))))))
(defun conjoin (fn &rest fns)
(if (null fns)
fn
(let ((conj (apply #conjoin fns)))
#(lambda (&rest args)
(and (apply fn args) (apply conj args))))))
(defun curry (fn &rest args)
#(lambda (&rest args2)
(apply fn (append args args2))))
(defun rcurry (fn &rest args)
#(lambda (&rest args2)
(apply fn (append args2 args))))
(defun always (x) #(lambda (&rest args) x))

. 6.2. Dylan

cddr = (compose #cdr #cdr)


nth = (compose #car #nthcdr)
atom = (compose #not #consp)
= (rcurry #typep atom)
<= = (disjoin #< #=)
listp = (disjoin #null #consp)
= (rcurry #typep list)
1+ = (curry #+ 1)
= (rcurry #+ 1)
1- = (rcurry #- 1)
mapcan = (compose (curry #apply #nconc) #mapcar)
complement = (curry #compose #not)

. 6.3.
122 6.

, :
#(lambda (&rest args) (a (b (apply #c args))))
, -
, -
.
, ,
, :
> (mapcar (compose #list #round #sqrt)
(4 9 16 25))
((2) (3) (4) (5))
, disjoin conjoin, -
. disjoin , -
, .
conjoin ,
-
.
> (mapcar (disjoin #integerp #symbolp)
(a "a" 2 3))
(T NIL T T)
> (mapcar (conjoin #integerp #oddp)
(a "a" 2 3))
(NIL NIL NIL T)
, , disjoin
, conjoin .
curry rcurry (right curry) -
make-adder.
,
. -
(make-adder 3):
(curry #+ 3)
(rcurry #+ 3)
curry rcurry , -
. curry ,
, :
> (funcall (curry #- 3) 2)
1
rcurry -
:
> (funcall (rcurry #- 3) 2)
-1
, always Common Lisp constanly.
, -
6.7. 123

. identity,
, -.

6.7.
2.11 -
. , -
, , ,
.1 -
,
, -
.
-
, , -
. -
. , , -
x:
(let ((x 10))
(defun foo ()
x))
x, , -
, ,
, x, :
> (let ((x 20)) (foo))
10
,
, , -
, . -
, special ,
. foo :
(let ((x 10))
(defun foo ()
(declare (special x))
x))
x -
, , ,
x, -
:
> (let ((x 20))
(declare (special x))

1
(scope) ; -
.
. . .
124 6.

(foo))
20
declare ,
. special , -
. 13 -
. ;
, .
, setf toplevel, -
:
> (setf x 30)
30
> (foo)
30

defparameter.
?
,
. ,
11 ,
*print-base*, 10.
( 16),
*print-base* :
> (let ((*print-base* 16))
(princ 32))
20
32
: princ , -
. ,
, *print-base* -
16, , let -
*print-base* 16.

6.8.
Common Lisp
. toplevel:
> (defun foo (x) (+ x 1))
FOO
. , -
, compiled-
function-p:
> (compiled-function-p #foo)
NIL
6.9. 125

, compile :
> (compile foo)
FOO

. -
, com-
piled-function-p.
compile . compi-
le . 171.
compile : stamp
reset, toplevel ( let)
1. ,
. -
, , -
.
,
compile-file. -
, , ,
. compiled-func-
tion-p .
,
. , make-adder (. 119), -
, :
> (compile make-adder)
MAKE-ADDER
> (compiled-funcion-p (make-adder 2))
T

6.9.
, .
:
1. . -
.
2. . -
. -
: nil,
cons, cdr .
3. . - -
, -
.

1
, ANSI Common Lisp, compile
.
126 6.

. , -
3.9,
,
.
, -
. -
,
. ,
:
1. , -
, .
2. , (
) .
, , -
. ,
, -
,
.
,
-
:
1. cdr, 1.
2. 0.
, .

.
-
. , -
,
. cdr ,
, .
.
. -
,
.
member ,
cdr . -
.
copy-tree , cons-, -
, copy-tree car copy-
tree cdr. copy-tree .
6.9. 127

,
.
-
, . , -
our-copy-tree (. 205) . -
, show-squares (. 40)
, (. 41). -
, -
.
-
, . -
, 13.2. -

.1
, , -
.
, , -
. -
. :
1. Fib(0) = Fib(1) = 1.
2. Fib(n) = Fib(n 1) + Fib(n 2).
:
(defun fib (n)
(if (<= n 1)
1
(+ (fib (- n 1))
(fib (- n 2)))))
. , -
. , -
(fib 10), (fib 9) (fib 8). (fib 9)
(fib 8), , -
.
:
(defun fib (n)
(do ((i n (- i 1))
(f1 1 (+ f1 f2))
(f2 1 f1))
((<= i 1) f1)))

1
, -
.
Scheme, Common Lisp. -
Common Lisp . . .
128 6.

, .
? ,
.
.


1. symbol-function -
. defun . ,
, -
setf.
2. ;
.
3. ,
.
4. . -
- .
5. , -
- . ,
.
, .
6. Dylan . -
Common
Lisp.
7. .
8. () -
.
9.
.

1. tokens (. 81), :test


:start, #constituent 0 .
2. bin-search (. 75), :key,
:test, :start :end, -
.
3. ,
.
4. most (. 118) , -
, .
5. remove-if ( ) filter
(. 117).
129

6. ,
.
7. ,
, , -
. -
nil.
8. , expensive
0 100 . -
. frugal, -
, expensive, expensive,
, .
9. apply, , -
,
(base 8).
7
7.

Common Lisp --
. , -
, read, .
, , -
format, .
-.
: .
; -
14.2.

7.1.
, /
. ,
.
, . - toplevel,
. ,
.
*standard-input*, -
*standard-output*. , ,
, toplevel.
read format .
toplevel. read -
,
*standard-input*. format
. format t, -
*standard-output*. ,
-
7.1. 131

.
.
(pathname) .
: , , , ,
. make-pathname
.
, :
> (setf path (make-pathname :name "myfile"))
#P"myfile"
open.
1, -
. , -
.
, -
. :direction ,
(:input), (:output) (:io).
, :if-exists -
, .
:supersede ( ). , ,
- "myfile":
> (setf str (open path :direction :output
:if-exists :supersede))
#<Stream C017E6>
-
Common Lisp.
str format,
, toplevel:
> (format str "Something~%")
NIL
, -
. -
,
:
> (close str)
NIL
.
, -
. "myfile", -
:
Something

1
, -
.
132 7.

,
:direction :input:
> (setf str (open path :direction :input))
#<Stream C01C86>
.
7.2. , -
read-line:
> (read-line str)
"Something"
NIL
> (close str)
NIL
.
open close . -
with-open-file.
, ,
open. -
, , -
.
. -
, :
(with-open-file (str path :direction :output
:if-exists :supersede)
(format str "Something~%"))
with-open-file unwind-protect (. 295), -
, ,
.

7.2.
read-line
read. ,
. , read, -
. , -
*standard-input*:
> (progn
(format t "Please enter your name: ")
(read-line))
Please enter your name: Rodrigo de Bivar
"Rodrigo de Bivar"
NIL
,
, . (
7.2. 133

t, read-line , -
.)
read-line -
: ; , , -
(eof); , , ,
. (. 242) -
.
, toplevel -
:
(defun pseudo-cat (file)
(with-open-file (str file :direction :input)
(do ((line (read-line str nil eof)
(read-line str nil eof)))
((eql line eof))
(format t "~A~%" line))))
, -
, read. -
. , -
. ,
(valid)
.
read toplevel,
:
> (read)
(a
b
c)
(A B C)
, -
, read , -
; -
read. ask-number (. 37).
, -
:
> (ask-number)
Please enter a number. a b
Please enter a number. Please enter a number. 43
43
.
read a, , -
. a
. read b,
, .
134 7.

, read .
: read-line
read-from-string. -
, :
> (read-from-string "a b c")
A
2
, -
.
read-from-string
. ,
read-line (
, , ).
:start :end , -
.

read-char, .
, read, read-line. Common
Lisp peek-char, read-char,
.

7.3.
: prin1, princ terpri.
; *standard-output*.
prin1 princ, , , prin1
, princ . , , prin1
, princ :
> (prin1 "Hello")
"Hello"
"Hello"
> (princ "Hello")
Hello
"Hello"
, , , -
, prin1. terpri -
.
,
format. format -
. ( t nil), -
. -
,
~ (). -
, format.
7.3. 135

t , *stan-
dard-output*. nil, format -
, .
. format
, . -
, -
. : ~A ~%.
( ~a ~A , -
, .) ~A
,
princ. ~% .
> (format nil "Dear ~A,~% Our records indicate"
"Mr. Malatesta")
"Dear Mr. Malatesta,
Our records indicate"
format ,
.
~S ~A, ,
prin1, princ:
> (format t "~S ~A" "z" "z")
"z" z
NIL
. ~F -
-
1 :
1. . -
.
2. . -
.
3. ( -
10). .
4. , , -
, .
, , ,
.
5. , . .

1
-
. -
, flo-
ating point numbers ( ). Common Lisp -
. . .
136 7.

, :
> (format nil "~10,2,0,*, F" 26.21875)
" 26.22"
(
0 , ), -
10 , -
. , -
*, #\*.
10 , -
.
. -
, -
. ,
, :
> (format nil "~,2,,,F" 26.21875)
"26.22"
, -
:
> (format nil "~,2F" 26.21875)
"26.22"
: , format
. (format nil "~,1F" 1.25) -
"1.2", "1.3". , -
(, ),
.

7.4. :
-
. -
, old
new.
old. ,
. ,
old, . . ,
new.
, -
? , , "abac",
"ababac".
: b, c.
, , a.
,
a .
7.4. : 137

, , -
, .
-
. -
, -
. . -
: -
, ,
, . -
, n -
, n , -
.
. 7.1 , .
buf : -
. , start end,
: start
; end

.
, used new,
. start
end, :
start used new end
used new start end -
. , used -
start, new end.
used . used new,
, ,
. -
,
, -
. , new, -
end,
.
bref ,
.
, ,
. (new-buf n) , n
.
, buf-insert.
end
. buf-pop, -
start. -
.
138 7.

(defstruct buf
vec (start -1) (used -1) (new -1) (end -1))
(defun bref (buf n)
(svref (buf-vec buf)
(mod n (length (buf-vec buf)))))
(defun (setf bref) (val buf n)
(setf (svref (buf-vec buf)
(mod n (length (buf-vec buf))))
val))
(defun new-buf (len)
(make-buf :vec (make-array len)))
(defun buf-insert (x b)
(setf (bref b (incf (buf-end b))) x))
(defun buf-pop (b)
(prog1
(bref b (incf (buf-start b)))
(setf (buf-used b) (buf-start b)
(buf-new b) (buf-end b))))
(defun buf-next (b)
(when (< (buf-used b) (buf-new b))
(bref b (incf (buf-used b)))))
(defun buf-reset (b)
(setf (buf-used b) (buf-start b)
(buf-new b) (buf-end b)))
(defun buf-clear (b)
(setf (buf-start b) -1 (buf-used b) -1
(buf-new b) -1 (buf-end b) -1))
(defun buf-flush (b str)
(do ((i (1+ (buf-used b)) (1+ i)))
((> i (buf-end b)))
(princ (bref b i) str)))

. 7.1.

-
: buf-next , buf-reset
used new start end. -
new , buf-next nil.
, nil
.
, buf-flush , -
, buf-clear ,
-1.
7.4. : 139

. 7.1 . 7.2, -
. file-subst -
: , , -
. , ,
stream-subst, .
, stream-subst, , -
. -
. -
, (1). -
, buf (2).

(defun file-subst (old new file1 file2)


(with-open-file (in file1 :direction :input)
(with-open-file (out file2 :direction :output
:if-exists :supersede)
(stream-subst old new in out))))
(defun stream-subst (old new in out)
(let* ((pos 0)
(len (length old))
(buf (new-buf len))
(from-buf nil))
(do ((c (read-char in nil :eof)
(or (setf from-buf (buf-next buf))
(read-char in nil :eof))))
((eql c :eof))
(cond ((char= c (char old pos))
(incf pos)
(cond ((= pos len) ; 3
(princ new out)
(setf pos 0)
(buf-clear buf))
((not from-buf) ; 2
(buf-insert c buf))))
((zerop pos) ; 1
(princ c out)
(when from-buf
(buf-pop buf)
(buf-reset buf)))
(t ; 4
(unless from-buf
(buf-insert c buf))
(princ (buf-pop buf) out)
(buf-reset buf)
(setf pos 0))))
(buf-flush buf out)))

. 7.2.
140 7.

pos . -
, , .
,
(3). -
,
, pos
, (4).
, -
"baro" "baric" , barbarous:


b b 2 b
a a 2 b a
r r 2 b a r
b o 4 b b.a r b.
a b 1 a a.r b.
r b 1 r r.b.
b b 1 r b:
a a 2 r b:a
r r 2 r b:a r
o o 3 baric
u b 1 u
s b 1 s

c;
,
; , , -
old pos; , -
;
. -
used new.
, .
, "test1",
:
The struggle between Liberty and Authority is the most conspicuous feature
in the portions of history with which we are earliest familiar, particularly
in that of Greece, Rome, and England.
(file-subst " th" " z" "test1" "test2") "test2"
:
The struggle between Liberty and Authority is ze most conspicuous feature in
ze portions of history with which we are earliest familiar, particularly in
zat of Greece, Rome, and England.
7.5. 141

, . 7.2. -
.
.
, , char=
.

7.5.
(macro character) ,
read. a b ,
-
.

(read-macro). Common Lisp
. , . , -
a, read (quote a).
toplevel
, .
, read :
> (car (read-from-string "a"))
QUOTE
, quote, ,
. -
.
.
(dispatching),
. -
- #.
. , # (function ...), -
(quote ...).
:
#(...) , #n(...) , #\
, #S(n ...) . prin1
( format ~S)
.1 , , ,
:
> (let ((*print-array* t))
(vectorp (read-from-string (format nil "~S"
(vector 1 2)))))
T

1
, -
*print-array*, t.
142 7.

, , ,
.
read-
table. -, , #<...>.
#< , read -
. - -
, , ,
1.
-
(, ) .
, -
#<...>.


1. .
.
2. , , toplevel. -
.
3. ,
.
4. format .
5. ,
.
6. read ,
.

1. , , -
.
2. , , -
.
3. -
%.
. , -

.

1
-
, .
143

4. ,
-
. -
10 . ( ,
.)
array-dimensions (. 374).
5. stream-subst ,
+. + old, -
.
6. stream-subst ,
, : , , -
.
. (: old .)
8
8.

- , -
, .
.
, , -
, . -
, .
, .

8.1.
2 ,
. ,
. -
, . -
symbol-name:
> (symbol-name abc)
"ABC"
, -
. Common Lisp ,
:
> (eql aBc Abc)
T
> (CaR (a b c))
A
, -
, ,
.
8.2. 145

, .
:
> (list |Lisp 1.5| || |abc| |ABC|)
(|Lisp 1.5| || |abc| ABC)

, .
, -
? , ,
, read -
. ,
, , .
-
, ,
.
, -
, :
> (symbol-name |a b c|)
"a b c"
( , -
\.)

8.2.
Common Lisp
(property-list, plist). get ,
, , :
> (get alizarin color)
NIL
eql.
, get nil.
, setf -
get:
> (setf (get alizarin color) red)
RED
> (get alizarin color)
RED
color () alizarin red (-
).
symbol-plist :
> (setf (get alizarin transparency) high)
HIGH
> (symbol-plist alizarin)
(TRANSPARENCY HIGH COLOR RED)
146 8.

,
, .
Common Lisp . -
-.

8.3. -
, ,
.
,
. .
, , ,
, , , .
,
, defstruct. ,
, , -
.
. 8.1.
,
. ,
, .
, :
.

name "FOO"
package
value 27
function #<function>
plist (color red)
package

. 8.1.

8.4.
8.1 , . -
. -
,
.
8.5. 147

, . -
. , -
, . -
, .
,
.
.
, -
( common-lisp-user). -
intern:
> (intern "RANDOM-SYMBOL")
RANDOM-SYMBOL
NIL
intern
( ). -
"RANDOM-SYMBOL",
. -
, .
. -
,
,
. -
gensym.
10.

8.5.
. -
, -
-
, .
, -
, , ,
,
. , -
, , ,
disp_, , -
, , math_. ,
, , ,
math_fft.
. -
. , -
, ,
( ), -
.
148 8.

, , , , math
disp. fft math, disp
math:fft. math -
fft.
, -
:
(defpackage "MY-APPLICATION"
(:use "COMMON-LISP" "MY-UTILITIES")
(:nicknames "APP")
(:export "WIN" "LOSE" "DRAW"))

(in-package my-application)
defpackage. my-applica-
tion1 , common-lisp my-utilities. -
, , ,
.
common-lisp, -
-
.
my-applications : win, lose
draw. defpackage
(nickname) app,
: app:win.
defpackage in-package,
my-application. , -
, my-application , in-
package . ,
in-package,
, .

8.6.
keyword ( ) -
:
:x keyword:x. -
(. 60), (member (a) ((a) (z)) test:
#equal) , (member (a) ((a)
(z)) :test #equal). , , -
, . -
.

1
, ,
, 8.1,
.
8.7. 149

? -
. , -
, ,
. ,
:
(defun noise (animal)
(case animal
(:dog :woof)
(:cat :meow)
(:pig :oink)))
,
, -
- case-.

8.7.
, -
: -
. ,
(. . 8.1).
symbol-value.
,
.
. , -
, -
. -
.
- -
(, ).
-
: , -
, .

8.8. :
, - -
, -
, . -
eql, -
string-equal string=.
. -
( , ), -
.
,
.
150 8.

, -
.
,
. , -
,
.
. 8.2 -
. - *words*.
, , -
:
((|sin| . 1) (|wide| . 2) (|sights| . 1))

(defparameter *words* (make-hash-table :size 10000))


(defconstant maxword 100)
(defun read-text (pathname)
(with-open-file (s pathname :direction :input)
(let ((buffer (make-string maxword))
(pos 0))
(do ((c (read-char s nil :eof)
(read-char s nil :eof)))
((eql c :eof))
(if (or (alpha-char-p c) (char= c #\))
(progn
(setf (aref buffer pos) c)
(incf pos))
(progn
(unless (zerop pos)
(see (intern (string-downcase
(subseq buffer 0 pos))))
(setf pos 0))
(let ((p (punc c)))
(if p (see p)))))))))
(defun punc (c)
(case c
(#\. |.|) (#\, |,|) (#\; |;|)
(#\! |!|) (#\? |?|) ))
(let ((prev |.|))
(defun see (symb)
(let ((pair (assoc symb (gethash prev *words*))))
(if (null pair)
(push (cons symb 1) (gethash prev *words*))
(incf (cdr pair))))
(setf prev symb)))

. 8.2.
8.8. : 151

|discover| Paradise
Lost 1. , discover
: sin sights -
wide.
read-text, -
. , -
buffer. maxword=100 -
, .
.
(
alpha-char-p) , . -
, -
, , see.
-
punc .
see . -
,
prev. , -
. see , prev
, .
read-text *words*
. -
hash-table-count. ,
, 10 000 .
. . 8.3
, . 8.2. -
generate-text.
() -
. , .
generate-text
random-text . random-text -
, prev .
-
.
.
, :
, -
Paradise Lost .

1
-
, 1667 . . .
.
152 8.

(defun generate-text (n &optional (prev |.|))


(if (zerop n)
(terpri)
(let ((next (random-next prev)))
(format t "~A " next)
(generate-text (1- n) next))))
(defun random-next (prev)
(let* ((choices (gethash prev *words*))
(i (random (reduce #+ choices
:key #cdr))))
(dolist (pair choices)
(if (minusp (decf i (cdr pair)))
(return (car pair))))))

. 8.3.


1. , ,
read, .
2. , -
, .
3. -
, .
4. . -
, , -
. .
5. , -
.
, -
, .
6. .
.
7. , , -
.

1. ,
eql?
2. ,
"FOO" foo.
153

3. defpackage, . 148,
.
. ?
4. . 7.1 , -
"RING". . 7.2
"FILE". .
5. , , -
Henley (. 8.8).
6. Henley,
, .
9
9.

( )
.
, .

9.1.
Common Lisp : -
, ,
. , ,
, , , -
, .
: 2001. -
, , , -
253.72, 2.5372e2 . -
: 2/3.
a+bi #c(a b), a b
.

integerp, floatp complexp. -
. 9.1.
, -
, :
1. -
, ( ,
). (+ 1.0 2) 3.0,
(+ #c(0 1.0) 2) #c(2.0 1.0).
2.
. (/ 10 2) 5.
9.2. 155

3. , , -
. , (+ #c(1 -1) #c(2 1))
3.
2 3 , -
:
> (list (ratiop 2/2) (complexp #c(1 0)))
(NIL NIL)

ratio
rational bignum
integer
fixnum bit
real
short-float
single-float
number float
double-float
long-float
complex

. 9.1.

9.2.
,
, . float -

:
> (mapcar #float (1 2/3 .5))
(1.0 0.66666667 0.5)
, -
, -
. -
truncate:
> (truncate 1.3)
1
0.2999995

. ( .00000005 -
.)
floor, ceiling round -
. floor,
, , ceiling,
, ,
156 9.

mirror? (. 62) -
:
(defun palindrome? (x)
(let ((mid (/ (length x) 2)))
(equal (subseq x 0 (floor mid))
(reverse (subseq x (ceiling mid))))))
truncate, floor ceiling
:
> (floor 1.5)
1
0.5
, truncate :
(defun our-truncate (n)
(if (> n 0)
(floor n)
(ceiling n)))
-
round. -
, Common Lisp, , , -
:
> (mapcar #round (-2.5 -1.5 1.5 2.5))
(-2 -2 2 2)
-
. , -
.1 -
, round -
.
mod floor,
rem truncate.
mod (. 107) -
(. 137).
signum, -
1, 0 -1 , : ,
.
abs. , (* (abs x) (signum x)) = x.
> (mapcar #signum (-2 -0.0 0.0 0 .5 3))
(-1 -0.0 0.0 0 1.0 1)
-0.0 ,
, . ,
-0.0 0.0.

1
format , -
. . . 136.
9.3. 157

, -
.
numerator
denominator. ( ,
, .)
realpart imagpart -
. ( , -
, .)
random . -
(random n) , -
n, , .

9.3.
=. -
, ,
:
> (= 1 1.0)
T
> (eql 1 1.0)
NIL
, eql, , -
.
: < (), <= ( ), = (),
>= ( ), > () /= ( ).
. ,
. , /=,
:
(<= w x y z)
:
(and (<= w x) (<= x y) (<= y z))
/= , -
, :
(/ w x y z)

(and (/= w x) (/= w y) (/= w z)
(/= x y) (/= x z) (/= y z))
zerop, plusp minusp,
, =,
> < . .
-0.0 ( ), ,
, = 0
158 9.

> (list (minusp -0.0) (zerop -0.0))


(NIL T)
zerop, minusp.
oddp evenp -
. , .
, , =, /= zerop -
.
-
max min , -
:
> (list (max 1 2 3 4 5) (min 1 2 3 4 5))
(5 1)
,
.

9.4.
+ -. -
,
0. (- n) n.
(- x y z)

(- (- x y) z)
, 1+ 1-, -
, 1 . -
1- , (1- x) x - 1, 1 - x.
incf decf
. (incf x n) , (setf
x (+ x n)), (decf x n) (setf x (- x n)). -
1.
*,
. -
, 1.
/ .
(/ n) (/ 1 n):
> (/ 3)
1/3
:
(/ x y z)

(/ (/ x y) z)
9.5. 159

/ .
, /
, :
> (/ 365 12)
365/12
, , -
, , toplevel . -
, ,
float:
> (float 365/12)
30.416666

9.5.
xn, (expt x n):
> (expt 2 5)
32
lognx, (log x n):
> (log 32 2)
5.0

.
e exp:
> (exp 2)
7.389056
log, -
:
> (log 7.389056)
2.0
expt,
:
> (expt 27 1/3)
3.0
sqrt:
> (sqrt 4)
2.0

9.6.
pi.
. sin, cos tan
160 9.

, -
:
> (let ((x (/ pi 4)))
(list (sin x) (cos x) (tan x)))
(0.7071067811865475d0 0.707167811865476d0 1.0d0)
-
.
asin, acos, atan.
, -1 1, asin acos -
.
, sinh,
cosh tanh . -
: asinh, acosh, atanh.

9.7.
Common Lisp -
. , -
, fixnum. -
, ,
bignum,
. , - -
, .
most-positive-fixnum most-negative-fixnum -
, -
bignum.
:
> (values most-positive-fixnum most-negative-fixnum)
536870911
-536870912
-
typep:
> (typep 1 fixnum)
T
> (typep (1+ most-positive-fixnum) bignum)
T
-
. Common Lisp -
: short-float, single-float, double-float long-float. -

( ).
short-float , . -
single-float double-float , ,
9.8. : 161


. long-float
, . -
4 .
,
: s, f, d l, e . (
, -
long-float, l 1.)
1.0 1L0.
-
. m-s-f, m
most least, s positive negative, f -
.
-
Common Lisp:
> (* most-positive-long-float 10)
Error: floating-point-overflow.

9.8. :
, ,
. -
,
.
.
-
: , -
, -
(image plane), .
, -
.
? -
-
.
: , , ;
,
. -
.
. 9.2 ,
. , sq, . ,
mag, x, y z.
. unit-vector
, , -
, :
162 9.

> (multiple-value-call #mag (unit-vector 23 12 47))


1.0
, mag distance, -
. (-
point :conc-name, nil. -
,
, , x point-x.)

(defun sq (x) (* x x))


(defun mag (x y z)
(sqrt (+ (sq x) (sq y) (sq z))))
(defun unit-vector (x y z)
(let ((d (mag x y z)))
(values (/ x d) (/ y d) (/ z d))))
(defstruct (point (:conc-name nil))
x y z)
(defun distance (p1 p2)
(mag (- (x p1) (x p2))
(- (y p1) (y p2))
(- (z p1) (z p2))))
(defun minroot (a b c)
(if (zerop a)
(/ (- c) b)
(let ((disc (- (sq b) (* 4 a c))))
(unless (minusp disc)
(let ((discrt (sqrt disc)))
(min (/ (+ (- b) discrt) (* 2 a))
(/ (- (- b) discrt) (* 2 a))))))))

. 9.2.

, minroot a, b c
x, ax2 + bx + c=0.
a , -
:

, -
, . 9.3. - ,
, ,
. (
.)
9.8. : 163

(defstruct surface color)


(defparameter *world* nil)
(defconstant eye (make-point :x 0 :y 0 :z 200))
(defun tracer (pathname &optional (res 1))
(with-open-file (p pathname :direction :output)
(format p "P2 ~A ~A 255" (* res 100) (* res 100))
(let ((inc (/ res)))
(do ((y -50 (+ y inc)))
((< (- 50 y) inc))
(do ((x -50 (+ x inc)))
((< (- 50 x) inc))
(print (color-at x y) p))))))
(defun color-at (x y)
(multiple-value-bind (xr yr zr)
(unit-vector (- x (x eye))
(- y (y eye))
(- 0 (z eye)))
(round (* (sendray eye xr yr zr) 255))))
(defun sendray (pt xr yr zr)
(multiple-value-bind (s int) (first-hit pt xr yr zr)
(if s
(* (lambert s int xr yr zr) (surface-color s))
0)))
(defun first-hit (pt xr yr zr)
(let (surface hit dist)
(dolist (s *world*)
(let ((h (intersect s pt xr yr zr)))
(when h
(let ((d (distance h pt)))
(when (or (null dist) (< d dist))
(setf surface s hit h dist d))))))
(values surface hit)))
(defun lambert (s int xr yr zr)
(multiple-value-bind (xn yn zn) (normal s int)
(max 0 (+ (* xr xn) (* yr yn) (* zr zn)))))

. 9.3.

-
surface. , , -
, .
surface color () 0
() 1 ().
x y.
z 200 .
164 9.

, *world* (-
nil). , z-
. 9.4
.

z


x

. 9.4.

tracer .
ASCII-, PGM.
100100. PGM- -
P2, , -
(100) (100) , -
255. 10 000
0 () 255 (), 100
100 .
res. res
, , 2, 200200 .
100100 -
. ,
. -
tracer color-at. -
, sendray,
. sendray -
0 1, -
0 255.
sendray ,
. first-hit, -
*world* , ,
, . -
, 0
(). ,
, . , -
, , -
N,
9.8. : 165

(, 1,
), -
L, :
i = NL
, N L -
, -
, 1. 90
, N L
0. , -
.
,
, , lambert, -
-
,
.
sendray (
),
. ,
. , , . 9.5.
sphere surface, sphere -
color, center radius. defsphere -
.
intersect () -
.
. intersect sphere-in-
tersect,
.
?
p = (x0, y0, z0) v = (xr, yr, zr). -
p + nv n , ,
(x0+nxr, y0+nyr, z0+nzr). ,
(xc, yc, zc) r. ,
:


166 9.

(defstruct (sphere (:include surface))


radius center)
(defun defsphere (x y z r c)
(let ((s (make-sphere
:radius r
:center (make-point :x x :y y :z z)
:color c)))
(push s *world*)
s))
(defun intersect (s pt xr yr zr)
(funcall (typecase s (sphere #sphere-intersect))
s pt xr yr zr))
(defun sphere-intersect (s pt xr yr zr)
(let* ((c (sphere-center s))
(n (minroot (+ (sq xr) (sq yr) (sq zr))
(* 2 (+ (* (- (x pt) (x c)) xr)
(* (- (y pt) (y c)) yr)
(* (- (z pt) (z c)) zr)))
(+ (sq (- (x pt) (x c)))
(sq (- (y pt) (y c)))
(sq (- (z pt) (z c)))
(- (sq (sphere-radius s)))))))
(if n
(make-point :x (+ (x pt) (* n xr))
:y (+ (y pt) (* n yr))
:z (+ (z pt) (* n zr))))))
(defun normal (s pt)
(funcall (typecase s (sphere #sphere-normal))
s pt))
(defun sphere-normal (s pt)
(let ((c (sphere-center s)))
(unit-vector (- (x c) (x pt))
(- (y c) (y pt))
(- (z c) (z pt)))))

. 9.5.

-
. ,
. , ;
,
( ); , -
, .
. -
n ,
9.8. : 167

n. min-
root. , sphere-intersect -
(x0 + nxr, y0 + nyr, z0 + nzr).
. 9.5, normal sphere-normal,
intersect sphere-intersect. -
,
.
. 9.6 , ;
ray-trace 38 ( ), -
, "spheres.pgm". -
res = 10 . 9.7.

(defun ray-test (&optional (res 1))


(setf *world* nil)
(defsphere 0 -300 -1200 200 .8)
(defsphere -80 -150 -1200 200 .7)
(defsphere 70 -100 -1200 200 .9)
(do ((x -2 (1+ x)))
((> x 2))
(do ((z 2 (1+ z)))
((> z 7))
(defsphere (* x 200) 300 (* z -400) 40 .75)))
(tracer (make-pathname :name "spheres.pgm") res))

. 9.6.

. 9.7. ,
168 9.

-
-
, -
.
. -
,
.
,
. ,
-. , . 9.3,
,
, -
.
, - -
. -
-, . -
inline
(. 13.3) -
.


1. Common Lisp , -
, .
2. ,
.
3. -
, /=, -
.
4. Common Lisp , -
. -
.
5. fixnum ,
. -
bignum. -
. Common Lisp 4- -
.
.
6. , -

.
169

1. ,
, -
.
2. , (-
) , -
, 25,
10, 5 1 , .
3. : -
. , .
, -
. -
:
1 2 3 4 5 6 7 8 9 10
6 5 6 4 5 5 4 5 6 5
4 5 4 6 5 5 6 5 4 5

, . -
,
.
4. , 8 , -
. -
nil, , -
: x- y- .
f , min max
; f (-
) i, min < i < max. -
, f, min, max epsilon
i epsilon.
5. .
ax3+bx2+cx+d, x(x(ax+b)+c)+d.
, -
x n , -
(n1).
x .
6.
fixnum?
7.
?
10
10.

- , -
. 2.3 , -
, -
. , -
.

10.1. Eval
, list.
: , -
? eval, -
:
> (eval (+ 1 2 3))
6
> (eval (format t "Hello"))
Hello
NIL
, eval .
toplevel:
(defun our-toplevel ()
(do ()
(nil)
(format t "~%> ")
(format (eval (read)))))
toplevel read-eval-print loop (
--).
eval
. :
10.1. Eval 171

1. : eval ,
, . -
, ,
.
2. - .
, , eval let, ,
eval, , let.
(.
). eval, -
, , - toplevel.
eval ,
. -
, eval cond:
(defun eval (expr env)
(cond ...
((eql (car expr) quote) (cadr expr))
...
(t (apply (symbol-function (car expr))
(mapcar #(lambda (x)
(eval x env))
(cdr expr))))))
, -
: , car, -
cdr (cdr)
1.
(quote x) ,
quote , -
. ,
quote.
, eval .
coerce compile
. - :
> (coerce (lambda (x) x) function)
#<Interpreted-Function BF9D96>
compile nil
-, :
> (compile nil (lambda (x) (+ x 2)))
#<Compiled-Function DF55BE>

1
eval, (env), -
. eval -
, ,
, Common Lisp -
.
172 10.

NIL
NIL
coerce compile -
, .
, -
, eval.
eval, coerce compile , -

(run-time). .
,
. -
, .

10.2.
,
, . ,
. , -
, .

(macro-expansion). ,
, , -
.
defmacro. defmacro -
defun, , -
, , -
. , ,
nil, :
(defmacro nil! (x)
(list setf x nil))
nil!,
. (nil! a) (setf a nil) -
. , (nil! x)
toplevel,
> (nil! x)
NIL
> x
NIL
(setf x nil).
, .
, . macroexpand1 -
:
10.3. 173

> (macroexpand1 (nil! x))


(SETF X NIL)
T
.
( toplevel) ,
, .
. -
, . , -
(nil! a) :
(lambda (expr)
(apply #(lambda (x) (list setf x nil))
(cdr expr)))
(setf a nil). defmacro,
. , macroexpand1, , car
, -
.

10.3.
(backquote) -
.
.
(), .
- .
, , -
:
> (a b c)
(A B C)
, -
.

, () ,@ (-).
, , . -
, :
> (setf a 1 b 2)
2
> (a is ,a and b is ,b)
(A IS 1 AND B IS 2)
list, -
, , -
. , nil!
:
174 10.

(defmacro nil! (x)


(setf ,x nil))
- ,
( ) :
> (setf lst (a b c))
(A B C)
> (lst is ,lst)
(LST IS (A B C))
> (its elements are ,@lst)
(ITS ELEMENTS ARE A B C)
, -
, , . -
, while, -
, :
> (let ((x 0))
(while (< x 10)
(princ x)
(incf x)))
0123456789
NIL
, ,
,
,@:
(defmacro while (test &rest body)
(do ()
((not ,test))
,@body))

10.4. :
. 10.1 1 , -
, , -
(quicksort).
:
1. . -
.
2. , -
, , -
, .
3. , -
, .

1
. 10.1 . -
. . .
10.5. 175

(defun quicksort (vec l r)


(let ((i l)
(j r)
(p (svref vec (round (+ l r) 2)))) ; 1
(while (<= i j) ; 2
(while (< (svref vec i) p) (incf i))
(while (> (svref vec j) p) (decf j))
(when (<= i j)
(rotatef (svref vec i) (svref vec j))
(incf i)
(decf j)))
(if (>= (- j l) 1) (quicksort vec l j)) ; 3
(if (>= (- r i) 1) (quicksort vec i r)))
vec)

. 10.1.

-
, , .
(. 10.1)
, . , -
, (p). -
-
, ,
, , -
. ( rotatef.)

, .
while, , quicksort
(. 10.1) when, incf, decf rotatef.
,
.

10.5.
-
, . -
-
. -
.
, -
, .
ntimes, n :
> (ntimes 10
(princ "."))
176 10.

..........
NIL
ntimes, -
, :
(defmacro ntimes (n &rest body) ; wrong
(do ((x 0 (+ x 1)))
((>= x ,n))
,@body))
.
,
.
, ,
. ,
, , -
, , -
. ntimes
x. ,
,
:
> (let ((x 10))
(ntimes 5
(setf x (+ x 1)))
x)
10
, x
15. x
do, setf -
, , .
, ,
:
(let ((x 10))
(do ((x 0 (+ x 1)))
((> x 5))
(setf x (+ x 1)))
x)

, . -
gensym (. 8.4). read -
, gensym ( -
eql) , -
. ntimes gensym
x, :
(defmacro ntimes (n &rest body) ; wrong
(let ((g (gensym)))
10.5. 177

(do ((,g 0 (+ ,g 1)))


((>= ,g ,n))
,@body)))
- -
: . -
do, -
. , -
:
> (let ((v 10))
(ntimes (setf v (- v 1))
(princ ".")))
.....
NIL
v 10 setf
, ,
.
,
:
(let ((v 10))
(do ((#:g1 0 (+ #:g1 1)))
((>= #:g1 (setf v (- v 1))))
(princ ".")))
(gensym
#:) 9, ,
.
,
.
gensym:
(defmacro ntimes (n &rest body)
(let ((g (gensym))
(h (gensym)))
(let ((,h ,n))
(do ((,g 0 (+ ,g 1)))
((>= ,g ,h))
,@body))))
ntimes .
-
, ,
. -
, .
, ,
, .
Common Lisp , -
.
178 10.

, . -
cond :
> (pprint (macroexpand-1 (cond (a b)
(c d e)
(t f))))
(IF A
B
(IF C
(PROGN D E)
F))
pprint, ,
.

10.6.
-
, , , -
, setf, -
setf. , , car:
(defmacro cah (lst) (car ,lst))
car setf, -
cah :
> (let ((x (list a b c)))
(setf (cah x) 44)
x)
(44 B C)
,
setf, , .
incf, :
(defmacro incf (x &optional (y 1)) ; wrong
(setf ,x (+ ,x ,y)))
. -
:
(setf (car (push 1 lst)) (1+ (car (push 1 lst))))

(incf (car (push 1 lst)))


lst nil, (2),
(1 2).
Common Lisp define-modify-macro
setf. -
: , (, -
, ) ,
10.7. : - 179

.
incf :
(define-modify-macro our-incf (&optional (y 1)) +)
push :
(define-modify-macro append1f (val)
(lambda (lst val) (append lst (list val))))
:
> (let ((lst (a b c)))
(append1f lst d)
lst)
(A B C D)
, push pop de-
fine-modify-macro, push
, pop -
.

10.7. : -
6.4 ,
. , -
. -
: nil!, ntimes, while.
,
.
-. . 10.2 ,
.
, for, while (. 174).
, :
> (for x 1 8
(princ x))
12345678
NIL
, do:
(do ((x 1 (1+ x)))
((> x 8))
(princ x))
for
do:
(do ((x 1 (1+ x))
(#:g1 8))
((> x #:g1))
(princ x))
180 10.

(defmacro for (var start stop &body body)


(let ((gstop (gensym)))
(do ((,var ,start (1+ ,var))
(,gstop ,stop))
((> ,var ,gstop))
,@body)))
(defmacro in (obj &rest choices)
(let ((insym (gensym)))
(let ((,insym ,obj))
(or ,@(mapcar #(lambda (c) (eql ,insym ,c))
choices)))))
(defmacro random-choice (&rest exprs)
(case (random ,(length exprs))
,@(let ((key -1))
(mapcar #(lambda (expr)
(,(incf key) ,expr))
exprs))))
(defmacro avg (&rest args)
(/ (+ ,@args) ,(length args)))
(defmacro with-gensyms (syms &body body)
(let ,(mapcar #(lambda (s)
(,s (gensym)))
syms)
,@body))
(defmacro aif (test then &optional else)
(let ((it ,test))
(if it ,then ,else)))

. 10.2.

-
. 8
, . -
gensym, -
.
. 10.2, in, , -
( eql)
. :
(in (car expr) + - *)
:
(let ((op (car expr)))
(or (eql op +)
(eql op -)
(eql op *)))
10.7. : - 181

, , -
, op gensym.
, random-choice,
.
(. 89). random-choice -
. :
(random-choice (turn-left) (turn-right))
:
(case (random 2)
(0 (turn-left))
(1 (turn-right)))
, with-gensyms,
.
, gensym -
. :
(let ((x (gensym)) (y (gensym)) (z (gensym)))
...)
:
(with-gensyms (x y z)
...)

. :
, . -
. , -
, -
- . -
avg, :
> (avg 2 4 8)
14/3
:
(defun avg (&rest args)
(/ (apply #+ args) (length args)))

. ,
( length) -
?
. 10.2, aif,
. -
it , if. -
:
182 10.

(let ((val (calculate-something)))


(if val
(1+ val)
0))
:
(aif (calculate-something)
(1+ it)
0)

. Common Lisp -
, next-method-p call-next-method.
-
, . for -
do.
? -
. -
, -
, . -
, -
.

, , .
- , -
, , -
, . -
. , , -
. :
? , , -
, , .

10.8.
, , , , -
, ,
. Common Lisp,
, ,
. 25 .
-
. -
. (
17.) ,
, .
.
,
183

.
- . , -
,
. , , . -
.
.


1. eval , -
.
2. , , -
. , , -
.
3. , , -
.
4. -
. , -
, pprint.
5. -
, setf.
6. -
. -
.
7. ,
.
.

1. x = a, y = b, z = (c d). -
, (x, y z) -
:
(a) ((C D) A Z)
(b) (X B C D)
(c) ((C D A) Z)
2. if cond.
3. , n -
. -
n- :
> (let ((n 2))
(nth-expr n (/ 1 0) (+ 1 2) (/ 1 0)))
3
184 10.

4. ntimes (. 175), () -
do.
5. n-of, n -
, -
n -
:
> (let ((i 0) (n 4))
(n-of n (incf i)))
(1 2 3 4)
6. , .

.
7. push?
(defmacro push (obj lst)
(setf ,lst (cons ,obj ,lst)))
,
, push.
8. , :
> (let ((x 1))
(double x)
x)
2
11
11. CLOS

Common Lisp (Common Lisp Object System, CLOS)


-
. . -
Com-
mon Lisp: defmethod , defun.

11.1. -

- -
. ,
. 1970 -
-
, . -
,
. , -
,
.
-
. -
, , -
, , -
.
, , -
. -
, -
. . 11.1.
186 11. CLOS

(defstruct rectangle
height width)
(defstruct circle
radius)
(defun area (x)
(cond ((rectangle-p x)
(* (rectangle-height x) (rectangle-width x)))
((circle-p x)
(* pi (expt (circle-radius x) 2)))))
> (let ((r (make-rectangle)))
(setf (rectangle-height r) 2
(rectangle-width r) 3)
(area r))
6

. 11.1.

CLOS , -
. 11.2. - -
,
. , . 11.2,
area, ,
. 11.1. area -
.
- -
,
. , -
defclass (. 11.2), ( -
). ,
, : circle colored:
(defclass colored ()
(color))

(defclass colored-circle (circle colored)


())
colored-circle, -
:
1. colored-circle : radius, -
circle, color colored.
2. colored-circle , area
,
circle.
11.2. 187

(defclass rectangle ()
(height width))
(defclass circle ()
(radius))
(defmethod area ((x rectangle))
(* (slot-value x height) (slot-value x width)))
(defmethod area ((x circle))
(* pi (expt (slot-value x radius) 2)))
> (let ((r (make-instance rectangle)))
(setf (slot-value r height) 2
(slot-value r width) 3)
(area r))
6

. 11.2.

- -
-
, , . -
? --
,
.
ob, display
. , ob,
, ob, -
,
.
ob, ,
ob . -
-
.

11.2.
4.6 : -
defstruct , -
make-point . -
.
defclass:
(defclass circle ()
(radius center))
circle, (-
), radius center.
188 11. CLOS

,
make-in-
stance, :
> (setf c (make-instance circle))
#<Circle #XC27496>
slot-value. -
setf:
> (setf (slot-value c radius) 1)
1
, -
.

11.3.
defclass .
, , ,
.
, , .
:accessor ,
, slot-value. -
circle :
(defclass circle ()
((radius :accessor circle-radius)
(center :accessor circle-center)))
circle-radius circ-
le-center :
> (setf c (make-instance circle))
#<Circle #XC5C726>
> (setf (circle-radius c) 1)
1
> (circle-radius c)
1
:accessor :writer :reader, -
, -
.
:initform.
make-instance ,
:initarg .1
, ,
:

1
, , ,
.
11.3. 189

(defclass circle ()
((radius :accessor circle-radius
:initarg :radius
:initform 1)
(center :accessor circle-center
:initarg :center
:initform (cons 0 0))))

:initform , :initarg
:
> (setf c (make-instance circle :radius 3))
#<Circle #XC2DE0E>
> (circle-radius c)
3
> (circle-center c)
(0 . 0)
, :initarg :initform.
, -
.
:allocation :class. (
:allocation :instance, , -
, .)
,
. -
, ,
.
, , -
.
,
. -
, . -
tabloid :
(defclass tabloid ()
((top-story :accessor tabloid-story
:allocation :class)))
-
, :
> (setf daily-blab (make-instance tabloid)
unsolicited-mail (make-instance tabloid))
#<Tabloid #XC2AB16>
> (setf (tabloid-story daily-blab) adultery-of-senator)
ADULTERY-OF-SENATOR
> (tabloid-story unsolicited-mail)
ADULTERY-OF-SENATOR
190 11. CLOS

:documentation, -
. :type,
, .
13.3.

11.4.
defclass .
. , screen-circle
circle graphic:
(defclass graphic ()
((color :accessor graphic-color :initarg :color)
(visible :accessor graphic-visible :initarg :visible
:initform t)))

(defclass screen-circle (circle graphic)


())
screen-circle , -
-. -
screen-circle
circle graphic.
(:accessor, :initarg ) -
,
circle graphic:
> (graphic-color (make-instance screen-circle
:color red :radius 3))
RED
-
, :initform:
(defclass screen-circle (circle graphic)
((color :initform purple)))
screen-circle :
> (graphic-color (make-instance screen-color))
PURPLE

11.5.
,
. -
, , -
.
-
,
. , , -
11.5. 191

, -
. :
(defclass sculpture () (height width depth))

(defclass statue (sculpture) (subject))

(defclas metalwork () (metal-type))

(defclass casting (metalwork) ())

(defclass cast-statue (statue casting) ())


, cast-statue ,
. 11.3.

standard-object

sculpture metalwork

statue casting

cast-statue

. 11.3.

, , -
, -. , , -
, -
defclass.
,
standard-object.
, (). , -
, standard-object,
t. , -
, . 11.3.
:
1. .
2. , .
192 11. CLOS

3. , , -
, , -
. , -
2.
4. t. ,
, .
3 : -
.
. 11.3 . -
: cast-statue, statue, sculp-
ture, casting, metalwork, standard-object, t. -
.
.
, -
. -
. , -
, . -
. 428.

11.6.
,
. defmethod,
defun:
(defmethod combine (x y)
(list x y))
combine , -
:
> (combine a b)
(A B)
, -
. .
, -
:
(defclass stuff () ((name :accessor name :initarg :name)))
(defclass ice-cream (stuff) ())
(defclass topping (stuff) ())
: stuff, name, ice-cream
topping, stuff.
combine:
(defmethod combine ((ic ice-cream) (top topping))
(format nil "~A ice-cream with ~A topping."
11.6. 193

(name ic)
(name top)))
defmethod :
. -
, .
:
ice-cream,
topping.
, ? ,
, . -
, , , ice-cream
topping , -
:
> (combine (make-instance ice-cream :name fig)
(make-instance topping :name treacle))
"FIG ice-cream with TREACLE topping."
:
> (combine 23 skiddoo)
(23 SKIDDOO)
,
,
. -
, otherwise case-.
.
:
(defmethod combine ((ic ice-cream) x)
(format nil "~A ice-cream with ~A."
(name ic)
x))
combine ice-cream topping,
:
> (combine (make-instance ice-cream :name grape)
(make-instance topping :name marshmallow))
"GRAPE ice-cream with MARSHMALLOW topping."
topping, -
:
> (combine (make-instance ice-cream :name clam)
reluctance)
"CLAM ice-cream with RELUCTANCE."
(app-
licable) . , -
, .
194 11. CLOS

.
, . ,

. . -

, , -
. -
, . .1
-
, :
ice-cream ice-cream, stuff, stan-
dard-object , , t.
,
, defclass.
( , ). -
combine :
(defmethod combine ((x number) (y number))
(+ x y))
, ,
eql:
(defmethod combine ((x (eql powder)) (y (eql spark)))
boom)
,
.
, -
, -
. -
,
, &rest, &key,
. :
(x) (a)
(x &optional y) (a &optional b)
(x y &rest z) (a b &key c)
(x y &key z) (a b &key c d)
:
(x) (a b)
(x &optional y) (a &optional b c)
(x &optional y) (a &rest b)
(x &key x y) (a)

1
- , -

. , -
.
11.7. 195

.
, -
.
, . :
(defmethod combine ((x (eql powder)) (y (eql spark)))
kaboom)
combine powder
spark.

11.7.
, be-
fore- (), after- () around- (). Before- -
: ,
. ,
. After- : P.S. -
. -
. ,
, , -
. ,
after-.
Before- after-
. Around- ,
. around-, -
. , -
( call-next-
method, ).
. -
:
1. around-,
.
2. :
(a) before- .
(b) .
(c) after- .
around- ( 1)
( 2).
-
(qualifier) defmethod. -
speak speaker :
(defclass speaker () ())

(defmethod speak ((s speaker) string)


196 11. CLOS

(format t "~A" string))


speak speaker -
:
> (speak (make-instance speaker)
"Im hungry")
Im hungry
NIL
intellectual, before- after--
speak:
(defclass intellectual (speaker) ())

(defmethod speak :before ((i intellectual) string)


(princ "Perhaps "))

(defmethod speak :after ((i intellectual) string)


(princ " in some sense"))
,
:
> (speak (make-instance intellectual)
"Im hungry")
Perhaps Im hungry in some sense
NIL
, before- after-. -
before- after- speaker:
(defmethod speak :before ((s speaker) string)
(princ "I think "))
:
> (speak (make-instance intellectual)
"Im hungry")
Perhaps I think Im hungry in some sense
NIL
, before- after-,
-
. format nil.
around-. -
, ,
around-. Around-
call-next-method. -
next-method-p.
around- , -
speaker:
(defclass courtier (speaker) ())
11.8. 197

(defmethod speak :around ((c courtier) string)


(format t "Does the King believe that ~A? " string)
(if (eql (read) yes)
(if (next-method-p) (call-next-method))
(format t "Indeed, it is a preposterous idea.~%"))
bow)
speak courtier
(), around-:
> (speak (make-instance courtier) "kings will last")
Does the King believe that kings will last? yes
I think kings will last
BOW
> (speak (make-instance courtier) "the world is round")
Does the King believe that the world is round? no
Indeed, it is a preposterous idea.
BOW
, , before- after-,
-
around-.

11.8.
, -
, (
call-next-method).

.
, -
.
,
-,
-
. price, -
+, price -
around-, , -
:
(defun price (&rest args)
(+ (apply args)
.
.
.
(apply args)))
around-, -
, . -
around- - call-next-me-
thod, .
198 11. CLOS


defgeneric:
(defgeneric price (x)
(:method-combination +))
price + ; -
defmethod- +.
:
(defclass jacket () ())
(defclass trousers () ())
(defclass suit (jacket trousers) ())

(defmethod price + ((jk jacket)) 350)


(defmethod price + ((tr trousers)) 200)
(suit), -
price:
> (price (make-instance suit))
550
:method-combination, defge-
neric ( defmethod), -
:
+ and append list max min nconc or progn
standard,
.
,
,
. ( :before :after)
. -
price, -
fmakunbound.

11.9.
-
, -
.
: ,
,
.
.
, -
, -
. -
11.10. 199

(. 120). stamp reset -


counter,
.
Common Lisp -
. -
, , , -
.

, . , -
counter increment clear:
(defpackage "CTR"
(:use "COMMON-LISP")
(:export "COUNTER" "INCREMENT" "CLEAR"))

(in-package ctr)

(defclass counter () ((state :initform 0)))

(defmethod increment ((c counter))


(incf (slot-value c state)))

(defmethod clear ((c counter))


(setf (slot-value c state) 0))
ctr -
counter increment clear,
state.
-
, ,
: (unintern)
, :
(unintern state)
.

11.10.
- -
- , :
. -
, .
-
, . , -
area:
tell obj area
200 11. CLOS

, obj -
.
. -
, move ,
. obj 10, -
:
tell obj move 10
:
(move obj 10)
:
.
,
.
,

.
, .

. , -
, -
, -
.


1. - f -
f . -
.
2.
. .
3. .
4. -
. .
5. . -
. -
, ,
.
6. . -
around--
, ; -
before-, , after-.
7. -
.
201

8. .
9. - -
.
.

1. accessor, initform, initarg , -


. 11.2. , -
slot-value.
2. . 9.5 , -
, introspect normal .
3. :
(defclass a (c d) ...) (defclass e () ...)
(defclass b (d c) ...) (defclass f (h) ...)
(defclass c () ...) (defclass g (h) ...)
(defclass d (e f g) ...) (defclass h () ...)
(a) , a,
, a
.
(b) b.
4. :
precedence: -
.
methods:
.
specializations: -
.
, (eql x), t ( -
).
( compute-applicab-
le-methods find-method) most-spec-app-meth,
, -
,
, .
5. . 11.2 , -
area . -
.
6. , ,
-
.
12
12.

3.3 ,
.
, . -
, . -
, , .

12.1.
. -
.
> (setf part (list b c))
(B C)
> (setf whole (cons a part))
(A B C)
( , cdr) .
, .
, ,
. 12.1.

part =

whole = nil

a b c

. 12.1.
12.1. 203

tailp.
, .
> (tailp part whole)
T
:
(defun our-tailp (x y)
(or (eql x y)
(and (consp y)
(our-tailp x (cdr y)))))

, nil .
,
. ,
, . 12.2. -
:
(setf part (list b c)
whole1 (cons 1 part)
whole2 (cons 2 part))
whole1 whole2 , -
.

part =

whole1 =

nil
1

whole2 =
b c

. 12.2.


. -
, ,
- , -
.
. 12.3.
, ,
. -
, . -
204 12.

, . 12.4, -
-:
(setf element (list a b)
holds1 (list 1 element 2)
holds2 (list element 3))

nil

a nil d

b c

. 12.3.

holds1 = nil

1 2
element = nil

a b

holds2 = nil

. 12.4.

holds1
holds2 ( , ), holds1 holds2
. -
,
, holds1 holds2.
-
. copy-list,
(defun our-copy-list (lst)
(if (null lst)
nil
(cons (car lst) (our-copy-list (cdr lst)))))
12.2. 205

,
. copy-tree, -
:
(defun our-copy-tree (tr)
(if (atom tr)
tr
(cons (our-copy-tree (car tr))
(our-copy-tree (cdr tr)))))
, -
. 12.5 co-
py-list copy-tree .

x (copy-list x) (copy-tree x)
nil nil nil

nil nil

. 12.5.

12.2.
?
-
,
. , , -
, . , -

.
,
:
(setf whole (list a b c)
tail (cdr whole))
tail whole,
, :
> (setf (second tail) e)
E
> tail
(B E)
206 12.

> whole
(A B E)
, , -
.
.
, . -
,
.
.
, ,
.
, .
,
setf ( pop, rplaca
) . ,
, , -
, -, .
, -
, .
, -
- . , ,
, :
1. .
2. -,
, .1
-
.
Common Lisp , -
(, - mapcar remove-if),
. -
.

12.3. :
. -
. ,
.
,

1
, Common Lisp ,
, . ,
intern - , -
, , intern,
.
12.3. : 207

, .
FIFO , (first
in, first out).
, -
.
, -
.
- .
. 12.6,
: a, b c. ,
. -
. , -
. ,
, cdr
.

q1 =

nil

a b

. 12.6.

. 12.7.

(defun make-queue () (cons nil nil))


(defun enqueue (obj q)
(if (null (car q))
(setf (cdr q) (setf (car q) (list obj)))
(setf (cdr (cdr q)) (list obj)
(cdr q) (cdr (cdr q))))
(car q))
(defun dequeue (q)
(pop (car q)))

. 12.7.

:
> (setf q1 (make-queue))
(NIL)
208 12.

> (progn (enqueue a q1)


(enqueue b q1)
(enqueue c q1))
(A B C)
q1 , . 12.6:
> q1
((A B C) C)
:
> (dequeue q1)
A
> (dequeue q1)
B
> (enqueue d q1)
(C D)

12.4.
Common Lisp , -
. -
. ,
, -
.
, delete remove.
, -
, . ,
:
> (setf lst (a r a b i a))
(A R A B I A)
> (delete a lst)
(R B I)
> lst
(A R B I)
remove, , -
setf:
(setf lst (delete a lst))
, , -
nconc, append.1
, , :
(defun nconc2 (x y)
(if (consp x)

1
n nconc non-consing. -
n.
12.5. : 209

(progn
(setf (cdr (last x)) y)
x)
y))
cdr
. -
B.
mapcan mapcar, -
( ) nconc:
> (mapcan #list
(a b c)
(1 2 3 4))
(A 1 B 2 C 3)
:
(defun our-mapcan (fn &rest lsts)
(apply #nconc (apply #mapcar fn lsts)))
mapcan , -
. nconc,
.
mapcan , , ,
. , children
- , -
:
(defun grandchildren (x)
(mapcan #(lambda (c)
(copy-list (children c)))
(children x)))
copy-list children,
,
.
mapcan:
(defun mappend (fn &rest lsts)
(apply #append (apply #mapcar fn lsts)))
mappend, copy-list -
grandchildren:
(defun grandchildren (x)
(mappend #children (children x)))

12.5. :
-
, . 4.7 , -
210 12.

(BST). -
, BST -
, .
. 12.8 bst-insert (. 86).
-
, . ,
, .

(defun bst-insert! (obj bst <)


(if (null bst)
(make-node :elt obj)
(progn (bsti obj bst <)
bst)))
(defun bsti (obj bst <)
(let ((elt (node-elt bst)))
(if (eql obj elt)
bst
(if (funcall < obj elt)
(let ((l (nodel bst)))
(if l
(bsti obj l <)
(setf (nodel bst)
(make-node :elt obj))))
(let ((r (node-r bst)))
(if r
(bsti obj r <)
(setf (node-r bst)
(make-node :elt obj))))))))

. 12.8. :

2.12 , -
.
bst-insert!, ,
bst-insert:
> (setf *bst* nil)
NIL
> (dolist (x (7 2 9 8 4 1 5 12))
(setf *bst* (bst-insert! x *bst* #<)))
NIL
push BST,
. (
. 429.)
12.5. : 211

. 12.91 bst-delete,
bst-remove (. 89) , delete remove.
delete, . -
bst-delete , bst-remove:
> (setf *bst* (bst-delete 2 *bst* #<))
#<7>
> (bst-find 2 *bst* #<)
NIL

(defun bst-delete (obj bst <)


(if (null bst)
nil
(if (eql obj (node-elt bst))
(del-root bst)
(progn
(if (funcall < obj (node-elt bst))
(setf (node-l bst) (bst-delete obj (node-l bst) <))
(setf (node-r bst) (bst-delete obj (node-r bst) <)))
bst))))
(defun del-root (bst)
(let ((l (node-l bst)) (r (node-r bst)))
(cond ((null l) r)
((null r) l)
(t (if (zerop (random 2))
(cutnext r bst nil)
(cutprev l bst nil))))))
(defun cutnext (bst root prev)
(if (node-l bst)
(cutnext (node-l bst) root bst)
(if prev
(progn
(setf (node-elt root) (node-elt bst)
(node-l prev) (node-r bst))
root)
(progn
(setf (node-l bst) (node-l root))
bst))))
(defun cutprev (bst root prev)
(if (node-r bst)
(cutprev (node-r bst) root bst)
(if prev
(progn
(setf (node-elt root) (node-elt bst)

1
bst-delete. -
. 89. . .
212 12.

(node-r prev) (node-l bst))


root)
(progn
(setf (node-r bst) (node-r root))
bst))))
(defun replace-node (old new)
(setf (node-elt old) (node-elt new)
(node-l old) (node-l new)
(node-r old) (node-r new)))
(defun cutmin (bst par dir)
(if (node-l bst)
(cutmin (node-l bst) bst :l)
(progn
(set-par par dir (node-r bst))
(node-elt bst))))
(defun cutmax (bst par dir)
(if (node-r bst)
(cutmax (node-r bst) bst :r)
(progn
(set-par par dir (node-l bst))
(node-elt bst))))
(defun set-par (par dir val)
(case dir
(:l (setf (node-l par) val))
(:r (setf (node-r par) val))))

. 12.9. :

12.6. :
. ,
:
, -
. , -
. ,
.
. 12.10 . cons-
: car, , cdr, -
. ,
. defstruct (. 12.10) -
, dl ( doubly linked),
. data
dl car cons-, next cdr.
12.6. : 213

prev cdr, . ( -
. 12.11.) ,
, nil.
defstruct ,
car, cdr consp: dl-data, dl-next dl-p.
dl->list , -
.
dl-insert cons. , , cons, -
-. cons, -
, . -
. -
, , -
,
prev .

(defstruct (dl (:print-function print-dl))


prev data next)
(defun print-dl (dl stream depth)
(declare (ignore depth))
(format stream "#<DL ~A>" (dl->list dl)))
(defun dl->list (lst)
(if (dl-p lst)
(cons (dl-data lst) (dl->list (dl-next lst)))
lst))
(defun dl-insert (x lst)
(let ((elt (make-dl :data x :next lst)))
(when (dl-p lst)
(if (dl-prev lst)
(setf (dl-next (dl-prev lst)) elt
(dl-prev elt) (dl-prev lst)))
(setf (dl-prev lst) elt))
elt))
(defun dl-list (&rest args)
(reduce #dl-insert args
:from-end t :initial-value nil))
(defun dl-remove (lst)
(if (dl-prev lst)
(setf (dl-next (dl-prev lst)) (dl-next lst)))
(if (dl-next lst)
(setf (dl-prev (dl-next lst)) (dl-prev lst)))
(dl-next lst))

. 12.10.
214 12.

nil nil

a b c

. 12.11.

,
. ,
.
dl-insert ,
.
- -
.
, .
, , -
. dl-insert,
cons, -
, .
dl-list dl- list.
dl:
> (dl-list a b c)
#<DL (A B C)>
reduce : :from-end, t,
:initial-value, nil,
:
(dl-insert a (dl-insert b (dl-insert c nil)))
#dl-insert #cons dl-list,
list:
> (setf dl (dl-list a b))
#<DL (A B)>
> (setf dl (dl-insert c dl))
#<DL (C A B)>
> (dl-insert r (dl-next dl))
#<DL (R A B)>
> dl
#<DL (C R A B)>
,
dl-remove. dl-insert, .
12.7. 215

12.7.
, .
. ,
. -
(cdr-circular), cdr-
.
, ,
cdr :
> (setf x (list a))
(A)
> (progn (setf (cdr x) x) nil)
NIL
x . . 12.12.

x= y= nil

. 12.12.

a
. , *print-circ-
le* t:
> (setf *print-circle* t)
T
> x
#1=(A . #1#)
#n= #n#
.
-
, , -
(1). -
:
(defun circular (lst)
(setf (cdr (last lst)) lst))

1
,
, .
. .
216 12.

(car-circular).
, -
. ,
, car .
, :
> (let ((y (list a)))
(setf (car y) y)
y)
#1=(#1#)
. 12.12. , -
(car-circular) - -
, (cdr-circular),
.
. car
cdr :
> (let ((c (cons 1 1)))
(setf (car c) c
(cdr c) c)
c)
#1=(#1# . #1#)
, -
. , , ,
,
, , -
, , -
, .
,
, :
> (setf *print-array* t)
T
> (let ((a (make-array 1)))
(setf (aref a 0) a)
a)
#1=#(#1#)
, , ,
.
, , defstruct, -
. , c, ,
parent, p,
child c:
> (progn (defstruct elt
(parent nil) (child nil))
(let ((c (make-elt))
(p (make-elt)))
12.8. 217

(setf (elt-parent c) p
(elt-child p) c)
c))
#1=#S(ELT PARENT #S(ELT PARENT NIL CHILD #1#) CHILD NIL)
*print-circle* t
.

12.8.
,
, -
, .
,
. ,
-
,
(defun arith-op (x)
(member x (+ - * /)))

. :
> (nconc (arith-op *) (as it were))
(* / AS IT WERE)
arith-op, -
:
> (arith-op as)
(AS IT WERE)
.
-
-.
arith-op -
. -
list
:
(defun arith-op (x)
(member x (list + - * /)))
list , -
member find:
(defun arith-op (x)
(find x (+ - * /)))
, , -
, :
218 12.

, , , . . -
-, .
,
- .
, -
, , .
, , -
(. 6.5).


1. .
, .
.
2. ,
, .
,
.
3. cons-, car -
, cdr .
4. -
.
5. -
.
6. .
.
7. , .

1. ,
((A) (A) (A)). , .
2. make-queue, enqueue dequeue (. . 12.7),
-
:
> (setf q (make-queue))
(NIL)
> (enqueue a q)
(A)
> (enqueu b q)
(A B)
> (dequeue q)
A
219

3. copy-queue, .
4. ,
.
5. ,
() (eql)
.
6. , -
, ,
.
7. , , -
.
8. , , -
.
13
13.

, : -
. -

,
, ,
.

- Common Lisp. -
, ,
. ,
, ,
, -
. , -
, ,
,
.

13.1.
, -
: ,
-
. , , -
, , ,
. , -
, -
, 3% .
,
, .
13.2. 221

-
, . -
, -

, , -
. , -
. -
, -
, .
, -
, , , -
.
: -
-
. : -
(, -
, ) . -
, , ,
. , , -
-
.
, , ,

.
: -
. ,
. -
. , -
, .
,
.
,
.
, . -
, , -
. -
, ,
, - . -
: -
.

13.2.
:
speed ( ), compilation-speed (
222 13.

), safety ( -
), space ( , )
debug ( , ).
-
. , -
: 0 ( ) 3 ( ). ,
. -
:
(defun bottleneck (...)
(do (...)
(...)
(do (...)
(...)
(declare (optimize (speed 3) (safety 0)))
...)))
, -
.
,
:
(declaim (optimize (speed 3)
(compilation-speed 0)
(safety 0)
(debug 0)))
. -
.1
, ,
.
speed, ,
.
, -
. :
(defun length/r (lst)
(if (null lst)
0
(1+ (length/r (cdr lst)))))
, -
1+.
:
(defun length/tr (lst)
(labels ((len (lst acc)
(if (null lst)

1
declaim. -
proclaim .
13.2. 223

acc
(len (cdr lst) (1+ acc)))))
(len lst 0)))
,
len, -
. -
, length/r, -
. acc,
.
goto,
.
, ,
len, , -
, . , -
,
:
, -
. ,
,
, .
.
-
(inline).
, -
. , -
, :
(declaim (inline single?))

(defun single? (lst)


(and (consp lst) (null (cdr lst))))


, -
. -
, , -
.
: Common Lisp -
.
Common Lisp -
. , toplevel,
. , toplevel -
, , , .
224 13.

single? inline--
1, single?
. :
(defun foo (x)
(single? (bar x)))
foo single? foo ,
:
(defun foo (x)
(let ((lst (bar x)))
(and (consp lst) (null (cdr lst)))))
inline- . -
. inline-
, -
, , .
,
(. 10.2). Common
Lisp .
-
. ,
, ,
disassemble. -
,
, .
,
-
: -
. -
, .
, ,
-
.

13.3.
, -
, , -
, : -
.

,
. .

1
inline- , , -
.
13.3. 225


: , -
,
. -
: ,
, .
2.15 , Common Lisp
, (manifest typing).1 -
, . -
.
, -
. +
, -
, -
.
, , -
, .
Common Lisp :
, . ,
fixnum, ,
, .
, -
.
, . Common Lisp
. , (, -
, ) .
declaim,
.
, ,
. , -
:
(declaim (type fixnum *count*))
ANSI Common Lisp type:
(declaim (fixnum *count*))
declare, -
, declaim. -
, : defun, lambda,

1
-
: . -
-
, (run-time typing) -
,
. , .
226 13.

let, do . , , -
fixnum, :
(defun poly (a b x)
(declare (fixnum a b x))
(+ (* a (expt x 2)) (* b x)))
, -
, .
the.
, , a, b x -
fixnum, , -
fixnum, :
(defun poly (a b x)
(declare (fixnum a b x))
(the fixnum (+ (the fixnum (* a (the fixnum (expt x 2))))
(the fixnum (* b x)))))
, ? , ,
-
the. -, . -, -
, -
.
Common Lisp ;
,
.
. , :
1. ,
( ).
, + fixnum
aref , -
.
2. , -
: fixnum simple-array
, integer sequence
.
-
, , .
, -
.
,
. -
, double-float, -
double-float. --
, . --
,
.
13.3. 227

x=

1.234d0 2.345d0 3.456d0

y=
1.234d0 2.345d0 3.456d0

. 13.1.

:element-type make-
array. . . 13.1 -
, -
:
(setf x (vector 1.234d0 2.345d0 3.456d0)
y (make-array 3 :element-type double-float)
(aref y 0) 1.234d0
(aref y 1) 2.345d0
(aref y 2) 3.456d0)

. -
, . -
x .
double-float,
. y -
double-float. -
, -
.
, y aref. -
simple-vector, -
svref.
, , -
. -
:
(declare (type (vector fixnum 20) v))
, v 20 -
fixnum.
, -
:
(declare (type (simple-array fixnum (4 4)) ar))
228 13.

ar 44, -
fixnum.
. 13.2 , 1 0001 000
single-float , .
(row-major
order). -
.

(setf a (make-array (1000 1000)


:element-type single-float
:initial-element 1.0s0))
(defun sum-elts (a)
(declare (type (simple-array single-float (1000 1000))
a))
(let ((sum 0.0s0))
(declare (type single-float sum))
(dotimes (r 1000)
(dotimes (c 1000)
(incf sum (aref a r c))))
sum))

. 13.2.

sum-elts ,
time. , -
,
. -
. sum-elts ,
, time -
:
> (time (sum-elts a))
User Run Time = 0.43 seconds
1000000.0
sum-elts,
:
> (time (sun-elts a))
User Run Time = 5.17 seconds
1000000.0
, -
, .
.
13.4. 229

13.4.
-
, . -
-
. ,
, .
(consing) -
. -
.
,
,
. -
.
,

, .
.
, .
, : .
, -
. -
. , -
. -
:

append nconc
reverse nreverse
remove delete
remove-if delete-if
remove-duplicates delete-duplicates
subst nsubst
subst-if nsubst-if
union nunion
intersection nintersection
set-difference nset-difference

, ,
delete remove, nreverse reverse . .
,
, .
,
230 13.

.

. -
. ,
.
, -
, ,
-
. Common Lisp
. fill-pointer
make-array. make-array , -
, fill-pointer, , -
:
> (setf *print-array* t)
T
> (setf vec (make-array 10 :fill-pointer 2
:initial-element nil))
#(NIL NIL)

:
> (length vec)
2
10. -
, vector-push vector-
pop, push pop :
> (vector-push a vec)
2
> vec
#(NIL NIL A)
> (vector-pop vec)
A
> vec
#(NIL NIL)
vector-push
.
, ,
make-array. -
, vector-push nil. vec
8 .

simple-vector, svref -
aref.
.
13.4. 231

-
. map-into
map. -
, , -
.
, .
:
(setf v (map-into v #1+ v))
. 13.3 ,
. ,
(, ).

(defconstant dict (make-array 25000 :fill-pointer 0))


(defun read-words (from)
(setf (fill-pointer dict) 0)
(with-open-file (in from :direction :input)
(do ((w (read-line in nil :eof)
(read-line in nil :eof)))
((eql w :eof))
(vector-push w dict))))
(defun xform (fn seq) (map-into seq fn seq))
(defun write-words (to)
(with-open-file (out to :direction :output
:if-exists :supersede)
(map nil #(lambda (x)
(fresh-line out)
(princ x out))
(xform #nreverse
(sort (xform #nreverse dict)
#string<)))))

. 13.3.

read-words ,
, write-words .
:
a amoeba alba samba marimba ...
:
... megahertz gigahertz jazz buzz fuzz
map-into,
.
232 13.

bignum. -
bignum -
. -
bignum, , -
fixnum.

, . , -
, -
dynamic extent.
dynamic-extent , , -
, .
? :
(defun our-reverse (lst)
(let ((rev nil))
(dolist (x lst)
(push x rev))
rev))
our-reverse
rev . -
, , , -
.
.
adjoin:
(defun our-adjoin (obj lst &rest args)
(if (apply #member obj lst args)
lst
(cons obj lst)))
, args -
. , . , -
dynamic-extent:
(defun our-adjoin (obj lst &rest args)
(declare (dynamic-extent args))
(if (apply #member obj lst args)
lst
(cons obj lst)))
( ) args ,
our-adjoin.

13.5. :
, -
, -
(pool). ,
13.5. : 233

, -
. , -
, ,
.
. 13.4 . *harbour*
,
ship. enter ;
find-ship ( ); leave
, .

(defparameter *harbor* nil)


(defstruct ship
name flag tons)
(defun enter (n f d)
(push (make-ship :name n :flag f :tons d)
*harbor*))
(defun find-ship (n)
(find n *harbor* :key #ship-name))
(defun leave (n)
(setf *harbor*
(delete (find-ship n) *harbor*)))

. 13.4.

,
. -
: -
, *harbour*
.
,
. . 13.5 -
, .
, , -
. *harbour* -, -
, .
ship
pool. ( :fill-pointer t,
.) enter
,
make-ship. leave -
, ,
.
234 13.

(defconstant pool (make-array 1000 :fill-pointer t))


(dotimes (i 1000)
(setf (aref pool i) (make-ship)))
(defconstant harbor (make-hash-table :size 1100
:test #eq))
(defun enter (n f d)
(let ((s (if (plusp (length pool))
(vector-pop pool)
(make-ship))))
(setf (ship-name s) n
(ship-flag s) f
(ship-tons s) d
(gethash n harbor) s)))
(defun find-ship (n) (gethash n harbor))
(defun leave (n)
(let ((s (gethash n harbor)))
(remhash n harbor)
(vector-push s pool)))

. 13.5. , 2

, -
. , -
, . , -

, , -
.

13.6.
, , , .
Common Lisp, ,
,
.
, :
elt, aref, svref.
. , ,
svref elt, , -
.
-
nth, elt. , length,
. Common Lisp -
? ,
13.6. 235

, .
, , ,
, .
eql eq.
, .
, -
, , -
. eq,
.
- , eq
( eq
fixnum). eq eql.
,
eq, .
, - eq (. . 13.5) -
. gethash
, . -
- . -
eq- eql-
, -
.
, eql- fixnum -
.
reduce apply,1
. ,
(apply #+ (1 2 3))
:
(reduce #+ (1 2 3))
, , -
. ,
.
-
, -
.
. , -
,
, 2. -

1
. -
, -
. . .
2
, , , (open co-
ding). . .
236 13.

.
,
.
-
. , case -
. ,

.

13.7.

, -
, -
. , -
-
, , -
.
Common Lisp , -
. -
,
.1

.
, -
. ,
, , .
,
, -
. .
-
, ,
.
, , -
. ,
.
, -
. .
-
. -

1
CFFI UFFI -
(foreign function interface),
. . .
237

, , -
. ,
, -
: . ,
.
.
, -
( ),
. -
. , -
,
, -
.
, . -
, -
.
, , .


1. .

.
2. . -
, .
3. ,
. -
.
4. , -
. -
.
5. -
, -
. , -
.
6. -
.
7. , -
.
8. -
, , -
.
238 13.

1. , inline-.
2. -
. -
.
(defun foo (x)
(if (zerop x)
0
(+ 1 (foo (1- x)))))
: .
3. . -
?
(a) 5.7.
(b) 9.8.
4. 3.15 , -
.
5. (. 4.7) -
.
14
14.

. -
. Common Lisp : -
. ,
-
, .

14.1.
Common Lisp . , , -
, integer. ,
type-of typep, -
, .
. -
integer.
t, .
. , nil : -
atom, list sequence.
, . ,
, ,
. , inte-
ger . -
,
.
, a b , (or a b)
, a
b. , (or a b),
a b.
240 14.

circular? , -
, -
:1
(or vector (and list (not (satisfies circular?))))
-
. , -
1 100 :
(integer 1 100)
, .
-
. * . ,
(simple-array fixnum (* *))
,
fixnum,
(simple-array fixnum *)
( ) , -
fixnum. -
, :
(simple-array fixnum)
, -
. , simple-array
.
-
, , -
deftype.
defmacro, , -
. :
(deftype proseq ()
(or vector (and list (not (satisfies circular?)))))
proseq:
> (typep #(1 2) proseq)
T
,
, defmacro. ,
(deftype multiple-of (n)
(and integer (satisfies (lambda (x)
(zerop (mod x ,n))))))

1
, , -
and or -
, and or.
14.2. 241

(multiple-of n) n:
> (typep 12 (multiple-of 4))
T
-
,
.

14.2.
7 -
. / , -
. integer
( unsigned-byte) :element-type
.
: read-
byte write-byte. -
:
(defun copy-file (from to)
(with-open-file (in from :direction :input
:element-type unsigned-byte)
(with-open-file (out to :direction :output
:element-type unsigned-byte)
(do ((i (read-byte in nil -1)
(read-byte in nil -1)))
((minusp i))
(declare (fixnum i))
(write-byte i out)))))
unsigned-byte :element-type, -
, -
-. , , 7--
, :element-type
(unsigned-byte 7)

14.3.
7.5 ,
read. ,
read, . -
, -
, .
set-macro-character -
. ,
read .
242 14.

, quote.
:
(set-macro-character #\
#(lambda (stream char)
(list (quote quote) (read stream t nil t))))
read ,
. ( -
,
.) read a, (quote a).
read. ,
read read. -
read :
; , t, , -
read ; -
,
( );
, t, read.
( make-dispatch-macro-character)
, #
, . , -
#, : #!, #?, #[,
#], #{ #}.

set-dispatch-macro-character,
set-macro-character, set-dispatch-macro-character ,
. #? ,
:
(setf-dispatch-macro-character #\# #\?
#(lambda (stream char1 char2)
(list quote
(let ((lst nil))
(dotimes (i (+ (read stream t nil t) 1))
(push i lst))
(nreverse lst)))))
#?n 0 n. -
:
> #?7
(0 1 2 3 4 5 6 7)
-
- . , -
, #{. -
:
(set-macro-character #\} (get-macro-character #\)))
14.4. 243

(set-dispatch-macro-character #\# #\{


#(lambda (stream char1 char2)
(let ((accum nil)
(pair (read-delimited-list #\} stream t)))
(do ((i (car pair) (+ i 1)))
((> i (cadr pair))
(list quote (nreverse accum)))
(push i accum)))))
#{x y}
x y :
> #{2 7}
(2 3 4 5 6 7)
read-delimited-list -
. , -
. } ,
set-macro-cha-
racter.
, -
,
eval-when -
. ,
, .

14.4.
, .
*package*.
Common Lisp common-lisp-user, -
. package-
name , find-package
:
> (package-name *package*)
"COMMON-LISP-USER"
> (find-package "COMMON-LISP-USER")
#<Package "COMMON-LISP-USER" 4CD15E>
, -
. symbol-package -
, .
> (symbol-package sym)
#<Package "COMMON-LISP-USER" 4CD15E>
, , -
, -
sym.
sym :
244 14.

> (setf sym 99)


99
:
> (setf *package* (make-package mine
:use (common-lisp)))
#<Package "MINE" 63390E>
,
, sym , :
MINE> sym
Error: SYM has no value.
? sym 99, -
, , , sym -
mine.1 sym , -
:
MINE> common-lisp-user::sym
99
, -
, . -
common-lisp-user, mine, . -
. -
, , -
- . - -
, , .
.
-
. - ,

, .
-
. -
, . -
, ,
- , .
.
(in-package
*package*) , ,
MINE> (in-package common-lisp-user)
#<Package "COMMON-LISP-USER" 4CD15E>
> (export bar)
T

1
Common Lisp -
toplevel, .
14.4. 245

> (setf bar 5)


5
. , -
mine, bar , -
:
> (in-package mine)
#<Package "MINE" 63390E>
MINE> common-lisp-user:bar
5
,
bar , mine -
:
MINE> (import common-lisp-user:bar)
T
MINE> bar
5
- -
. , ,
mine:bar.
, ?
, , , -
sym:
MINE> (import common-lisp-user::sym)
Error: SYM is already present in MINE.
sym mine, -
, mine.
, ,
, , ,
mine. ,
sym mine, .
-
:
MINE> (use-package common-lisp-user)
T
, , -
mine. ( sym ,
.)
common-lisp ,
. :use -
make-package, mine, -
Common Lisp:
246 14.

MINE> #cons
#<Compiled-Function CONS 462A3E>
, -
toplevel.
. def-
package in-package, . 148.
, , .
, . , common-
lisp, cons, common-lisp -
.
cons , common-lisp.
, ,
, .

14.5. Loop
loop
- , -
,
, -. , loop
, -
: ,
, ,
.
loop ,
: . , -
: , .
, , , ,
, ANSI
.
loop , -
( ) -
. ANSI , loop,
, , -
.
, loop, -
. , , -
. -
. , loop- - .
, loop : -
, , -
, .
loop- , -
.
:
14.5. Loop 247

1. . loop; -
.
2. . .
, ,
.
3. . ; -
.
loop- ,
. :
> (loop for x from 0 to 9
do (princ x))
0123456789
NIL
0 9 nil.
:
for x from 0 to 9
: x
0 , 9
.
do (princ x)
(princ-) .
for
. -
- while until:
> (loop for x = 8 then (/ x 2)
until (< x 1)
do (princ x))
8421
NIL
and for, -
:
> (loop for x from 1 to 4
and y from 1 to 4
do (princ (list x y)))
(1 1) (2 2) (3 3) (4 4)
NIL
for , -
.
, ,
- . :
248 14.

> (loop for x in (1 2 3 4)


collect (1+ x))
(2 3 4 5)
in from for-
, -
.
collect .
, nil;
(1+ x) 1,
.
, - -
. -
, collect.
mapcar.
loop -
:
> (loop for x from 1 to 5
collect (random 10))
(3 8 6 5 0)
.
map-int (. 117).
, loop?
: loop, map-int?
collect -
. -
:
(defun even/odd (ns)
(loop for n in ns
if (evenp n)
collect n into evens
else collect n into odds
finally (return (values evens odds))))
finally . -
.
sum collect,
, . 1 n :
(defun sum (n)
(loop for x from 1 to n
sum x))
loop D, -
. 331. : . 14.1
, . 14.2
loop.
14.5. Loop 249

(defun most (fn lst)


(if (null lst)
(values nil nil)
(let* ((wins (car lst))
(max (funcall fn wins)))
(dolist (obj (cdr lst))
(let ((score (funcall fn obj)))
(when (> score max)
(setf wins obj
max score))))
(values wins max))))
(defun num-year (n)
(if (< n 0)
(do* ((y (- yzero 1) (- y 1))
(d (- (year-days y)) (- d (year-days y))))
((<= d n) (values y (- n d))))
(do* ((y yzero (+ y 1))
(prev 0 d)
(d (year-days y) (+ d (year-days y))))
((> d n) (values y (- n prev))))))

. 14.1. loop

loop- , -
. , even/odd finally
, collect-
. -
loop.
:
(loop for y = 0 then z
for x from 1 to 5
sum 1 into z
finally (return (values y z)))

(loop for x from 1 to 5


for y = 0 then z
sum 1 into z
finally (return (values y z)))

. ?
? -
. loop ,

.
loop . ,
, , ,
250 14.

, . 14.2, -
.

(defun most (fn lst)


(if (null lst)
(values nil nil)
(loop with wins = (car lst)
with max = (funcall fn wins)
for obj in (cdr lst)
for score = (funcall fn obj)
when (> score max)
do (setf wins obj
max score)
finally (return (values wins max)))))
(defun num-year (n)
(if (< n 0)
(loop for y downfrom (- yzero 1)
until (<= d n)
sum (- (year-days y)) into d
finally (return (values (+ y 1) (- n d))))
(loop with prev = 0
for y from yzero
until (> d n)
do (setf prev d)
sum (year-days y) into d
finally (return (values (- y 1)
(- n prev))))))

. 14.2. loop

14.6.
Common Lisp (conditions)
, . -
- , -
. - -
. Common Lisp
. -
.
-
.
, ,
, .
14.6. 251

Common Lisp -
. error.
, format:
> (error "Your report uses ~A as a verb." status)
Error: Your report uses STATUS as verb.
Options: :abort, :backtrace
>>
, ,
.

ecase, check-type assert. case,
, :
> (ecase 1 (2 3) (4 5))
Error: No applicable clause.
Options: :abort, :backtrace
>>
case- nil, , -
, -
ecase, otherwise.
check-type , -
-. , -
.
:
> (let ((x (a b c)))
(check-type (car x) integer "an integer")
x)
Error: The value of (CAR X), A, should be an integer.
Options: :abort, :backtrace, :continue
>> :continue
New value of (CAR X)? 99
(99 B C)
>
,
(car x), -
, .
assert. -
, -
, error:
> (let ((sandwich (ham on rye)))
(assert (eql (car sandwich) chicken)
((car sandwich))
"I wanted a ~A sandwich." chicken)
sandwich)
252 14.

Error: I wanted a CHICHEN sandwich.


Options: :abort, :backtrace, :continue
>> :continue
New value of (CAR SANDWICH)? chicken
(CHICKEN ON RYE)
>
,
. -
, ignore-errors. progn,
.
,
, ignore-errors
: nil , -
.
, -
, , -
, -
:
(defun user-input (prompt)
(format t prompt)
(let ((str (read-line)))
(or (ignore-errors (read-from-string str))
nil)))
nil, -
:
> (user-input "Please type an expression> ")
Please type an expression> #%@#+!!
NIL
15
15. :

-
. , ,
, -
, .
, -
, -. -
, , -
-
. , -
.

15.1.
-
: .
:
(parent donald nancy)
,
, .
:
(<- )
, .
, -
. , :
(<- (child ?x ?y) (parent ?y ?x))
254 15. :

, y x, x y,
, , (child x y)
(parent y x).
( ) ,
and, or, not. , ,
, x y, x y, -
:
(<- (father ?x ?y) (and (parent ?x ?y) (male ?x)))
. , , -
, x y, -
:
(<- (daughter ?x ?y) (and (child ?x ?y) (female ?x)))

, . -
(backward
chaining). , --
, , , -
-. ,
, ( -
) , , , ,
.

15.2.

, , -
(pattern-matching). ,
, , , -
, .
, ?x ?y , :
(p ?x ?y c ?x)
(p a b c a)
, ?x = a ?y = b,
(p ?x b ?y a)
(p ?y b c a)
?x = ?y = c.
. 15.1 match, .
, , -
, :
> (match (p a b c a) (p ?x ?y c ?x))
((?Y . B) (?X . A))
T
15.2. 255

> (match (p ?x b ?y a) (p ?y b c a))


((?Y . C) (?X . ?Y))
T
> (match (a b c) (a a a)
NIL
match -
, (bindings), -
binds. , match -
, nil.
- -
, match, , gethash, -
, .
> (match (p ?x) (p ?x))
NIL
T

(defun match (x y &optional binds)


(cond
((eql x y) (values binds t))
((assoc x binds) (match (binding x binds) y binds))
((assoc y binds) (match x (binding y binds) binds))
((var? x) (values (cons (cons x y) binds) t))
((var? y) (values (cons (cons y x) binds) t))
(t
(when (and (consp x) (consp y))
(multiple-value-bind (b2 yes)
(match (car x) (car y) binds)
(and yes (match (cdr x) (cdr y) b2)))))))
(defun var? (x)
(and (symbolp x)
(eql (char (symbol-name x) 0) #\?)))
(defun binding (x binds)
(let ((b (assoc x binds)))
(if b
(or (binding (cdr b) binds)
(cdr b)))))

. 15.1.

match nil t, , ,
, -
. match :
1. x y eql, ; :
2. x , ,
y, ; :
256 15. :

3. y , ,
x, ; :
4. x , -
; :
5. y , -
; :
6. , cons-, car-
, cdr -
.
, :
> (match (p ?v b ?x d (?z ?z))
(p a ?w c ?y ( e e))
((?v . a) (?w . b)))
((?Z . E) (?Y . D) (?X . C) (?V . A) (?W . B))
T
( ), match -
binding. , -
, -
, ?x a
, (?x . ?y) (?y . a).
> (match (?x a) (?y ?y))
((?Y . A) (?X . ?Y))
T
?x ?y, ?y a, ?x
.

15.3.
, ,
: -
, , , -
,
. , ,
(parent donald nancy)

(parent ?x ?y)
,
(((?x . donald) (?y . nancy)))
,
: ?x donald, ?y nancy.
15.3. 257

, ,
. -
. . 15.2.
- *rules* .
, -
. , -
, -
.

(defvar *rules* (make-hash-table))


(defmacro <- (con &optional ant)
(length (push (cons (cdr ,con) ,ant)
(gethash (car ,con) *rules*))))

. 15.2.

<- , .
, .
. ,
,
, . :
> (<- (parent donald nancy))
1
> (<- (child ?x ?y) (parent ?y ?x))
1
<- ;
length push
, toplevel.
. 15.3 , .
prove , -
. .
,
prove-simple. . -

, .
-
, match. prove ,
mapcan:
> (prove-simple parent (donald nancy) nil)
(NIL)
> (prove-simple child (?x ?y) nil)
(((#:?6 . NANCY) (#:?5 . DONALD) (?Y . #:?5) (?X . #:?6)))
258 15. :

(defun prove (expr &optional binds)


(case (car expr)
(and (prove-and (reverse (cdr expr)) binds))
(or (prove-or (cdr expr) binds))
(not (prove-not (cadr expr) binds))
(t (prove-simple (car expr) (cdr expr) binds))))
(defun prove-simple (pred args binds)
(mapcan #(lambda (r)
(multiple-value-bind (b2 yes)
(match args (car r) binds)
(when yes
(if (cdr r)
(prove (cdr r) b2)
(list b2)))))
(mapcar #change-vars
(gethash pred *rules*))))
(defun change-vars (r)
(sublis (mapcar #(lambda (v) (cons v (gensym "?")))
(vars-in r))
r))
(defun vars-in (expr)
(if (atom expr)
(if (var? expr) (list expr))
(union (vars-in (car expr))
(vars-in (cdr expr)))))

. 15.3.

, -
. ( , -
nil.) - ,
?x ?y (-
) nancy donald.
,
(. 39) :
, .
, gensym. -
, , -
. :
(<- (child ?x ?y) (parent ?y ?x))

(<- (daughter ?y ?x) (and (child ?y ?x) (female ?y)))


, x y 1) x y,
y x; 2) y x, y -
x y . -
15.3. 259

, ,
, .
,
, . -
, a b,
?y = a ?x = b. ,
:
> (match (child ?y ?x)
(child ?x ?y)
((?y . a) (?x . b)))
NIL
,
, gensym.
change-vars. -

. , ,
.
change-vars ,
.
-
. . 15.4. -
or not . -
, or.
, -
not nil.

(defun prove-and (clauses binds)


(if (null clauses)
(list binds)
(mapcan #(lambda (b)
(prove (car clauses) b))
(prove-and (cdr clauses) binds))))
(defun prove-or (clauses binds)
(mapcan #(lambda (c) (prove c binds))
clauses))
(defun prove-not (clause binds)
(unless (prove clause binds)
(list binds)))

. 15.4.

prove-and . , -
,
. and -
260 15. :

. prove-
and ( -
, -
).
, -
. , prove, -
, -
. with-answer, . 15.5.
( ) -
, , -
,
:
> (with-answer (parent ?x ?y)
(format t "~A is the parent of ~A.~%" ?x ?y))
DONALD is the parent of NANCY.
NIL
-
prove . . 15.6 -
, . 15.7
.

(defmacro with-answer (query &body body)


(let ((binds (gensym)))
(dolist (,binds (prove ,query))
(let ,(mapcar #(lambda (v)
(,v (binding ,v ,binds)))
(vars-in query))
,@body))))

. 15.5.

(with-answer (p ?x ?y)
(f ?x ?y))
;;; :
(dolist (#:g1 (prove (p ?x ?y)))
(let ((?x (binding ?x #:g1))
(?y (binding ?y #:g1)))
(f ?x ?y)))

. 15.6. with-answer
15.4. 261

(clrhash *rules*)
:
(<- (parent donald nancy))
(<- (parent donald debbie))
(<- (male donald))
(<- (father ?x ?y) (and (parent ?x ?y) (male ?x)))
(<- (= ?x ?y))
(<- (sibling ?x ?y) (and (parent ?z ?x)
(parent ?z ?y)
(not (= ?x ?y))))
:
> (with-answer (father ?x ?y)
(format t "~A is the father of ~A.~%" ?x ?y))
DONALD is the father of DEBBIE.
DONALD is the father of NANCY.
NIL
> (with-answer (sibling ?x ?y)
(format t "~A is the sibling of ~A.~%" ?x ?y))
DEBBIE is the sibling of NANCY.
NANCY is the sibling of DEBBIE.
NIL

. 15.7.

15.4.
, -
. , -
. , , , -
, .
, .
<- with-answer.
-
,
. ( avg . 181.)
, .
prove prove-and, -
, . -
.
, , -
? with-answer,
<- .
262 15. :

, -
, -
- . , -
, On Lisp
Paradigms of Artificial Intelligence Programming1, -
, .

1
Peter Norvig Paradigms of Artificial Intelligence Programming, Morgan
Kaufman, 1992. , PAIP, -
. Com-
mon Lisp , -
: http://norvig.com/paip/README.html. . .
16
16. : HTML

HTML- , -
-. -
,
-. HTML--
, ,
.

16.1. HTML
HTML (HyperText Markup Language, )
, -. ,
, . -
HTML.
- -, -
( , ), -
. HTML- -
, ,
.
. 16.1 HTML-, . 16.2 -
, . ,
. .
HTML . :
<> ... </>
, -
. <h2>. ,
<h2> </h2>, .
( <h1>.)
264 16. : HTML

<center>
<h2>Your Fortune</h2>
</center>
<br><br>
Welcome to the home page of the Fortune Cookie
Institute. FCI is a non-profit institution
dedicated to the development of more realistic
fortunes. Here are some examples of fortunes
that fall within our guidelines:
<ol>
<li>Your nostril hairs will grow longer.
<li>You will never learn how to dress properly.
<li>Your car will be stolen.
<li>You will gain weight.
</ol>
Click <a href="research.html">here</a> to learn
more about our ongoing research projects.

. 16.1. HTML-

Your Fortune
Welcome to the home page of the Fortune Cookie Institute. FCI is a
nonprofit institution dedicated to the development of more realistic
fortunes. Here are some examples of fortunes that fall within our
guidelines:
1. Your nostril hairs will grow longer.
2. You will never learn how to dress properly.
3. Your car will be stolen.
4. You will gain weight.
Click here to learn more about our ongoing research projects.

. 16.2. -

: <ol> (ordered list, )


; <center> ;
<a...> (anchor, ) .
. ,
<a...> </a>, , -
, . -
, . -

16.2. HTML 265

<a href="foo.html">
HTML-, -
. , . 16.2 -
"research.html".
,
(
).
.
(. . 16.1) <br> (break, )
<li> (list item, ) -
. , HTML -
, ,
. 16.1.

16.2. HTML
HTML.
. 16.3 .
*standard-output*, -
, .

(defmacro as (tag content)


(format t "<~(~A~)>~A</~(~A~)>"
,tag ,content ,tag))
(defmacro with (tag &rest body)
(progn
(format t "~&<~(~A~)>~%" ,tag)
,@body
(format t "~&<~(~A~)>~%" ,tag)))
(defun brs (&optional (n 1))
(fresh-line)
(dotimes (i n)
(princ "<br>"))
(terpri))

. 16.3.

as with , -
. -
:
> (as center "The Missing Lambda")
<center>The Missing Lambda</center>
NIL
266 16. : HTML

-
:
> (with center
(princ "The Unbalanced Parenthesis"))
<center>
The Unbalanced Parenthesis
</center>
NIL
~( ~) -
. , HTML
, , -
.
as -
, with . ( ~&
.) -
, HTML- .
.
. 16.3, brs, -
. -
.
16.4 HTML.
. -
-
. ".html".
page . with-open-
file, . -
*standard-output* ,
, name.

(defun html-file (base)


(format nil "~(~A~).html" base))
(defmacro page (name title &rest body)
(let ((ti (gensym)))
(with-open-file (*standard-output*
(html-file ,name)
:direction :output
:if-exists :supersede)
(let ((,ti ,title))
(as title ,ti)
(with center
(as h2 (string-upcase ,ti)))
(brs 3)
,@body))))

. 16.4.
16.2. HTML 267

6.7 , -
. . 124 -
*print-base* 16 let. page -
*standard-output* , HTML-.
as princ page, -
.
title . -
. , :
(page paren "The Unbalanced Parenthesis"
(princ "Something in his expression told her..."))
, "paren.html" ( html-file
) :
<title>The Unbalanced Parenthesis</title>
<center>
<h2>THE UNBALANCED PARENTHESIS</h2>
</center>
<br><br><br>
Something in his expression told her...
, <title>. , -
<title>, , -
.

(defmacro with-link (dest &rest body)


(progn
(format t "<a href=\"~A\">" (html-file ,dest))
,@body
(princ "</a>")))
(defun link-item (dest text)
(princ "<li>")
(with-link dest
(princ text)))
(defun button (dest text)
(princ "[ ")
(with-link dest
(princ text))
(format t " ]~%"))

. 16.5.

. 16.5 .
with-link with.
, HTML-, -
:
268 16. : HTML

> (with-link capture


(princ "The Captured Variable"))
<a href="capture.html">The Captured Variable</a>
"</a>"
link-item, -
, ,
> (link-item bq "Backquote!")
<li><a href="bq.html"> Backquote!</a>
"</a>"
button,
,
> (button help "Help")
[ <a href="help.html">Help</a> ]
NIL
.

16.3.
,
. , -
? , .

- , , , -
, .

.
,
, . -
.
(. 16.6) mapc. -
.
, -
. ( -
, nil.)
> (map3 #(lambda (&rest args) (princ args))
(a b c d))
(A NIL B) (B A C) (C B D) (D C NIL)
NIL
mapc, nil.1 ,
, . -

1
, mapc . .
.
16.4. 269

, -
.

(defun map3 (fn lst)


(labels ((rec (curr prev next left)
(funcall fn curr prev next)
(when left
(rec (car left)
curr
(cadr left)
(cdr left)))))
(when lst
(rec (car lst) nil (cadr lst) (cdr lst)))))

. 16.6.

, -
- :
> (map3 #(lambda (c p n)
(princ c)
(if n (princ " | ")))
(a b c d))
A | B | C | D
NIL

, ,
, , -
.

16.4.
, - -
. ,
, , , , . . -
, -
.
, --
, .
, , , -
, , -
. -
, .
-
. , , ,
.
270 16. : HTML

, -
. , -
, . -
. 16.7.

section item

contents section item index

section item

. 16.7.

, ,
. 16.8. -
: , , ,
.

(defparameter *sections* nil)


(defstruct item
id title text)
(defstruct section
id title items)
(defmacro defitem (id title text)
(setf ,id
(make-item :id ,id
:title ,title
:text ,text)))
(defmacro defsection (id title &rest items)
(setf ,id
(make-section :id ,id
:title ,title
:items (list ,@items))))
(defun defsite (&rest sections)
(setf *sections* sections))

. 16.8.

, id.
, .
defitem defsection; id -
, .
16.4. 271

: id , -
. , , foo,
"foo.html".
, title,
-
.

defsection,
defsite.
. 16.9. , -
. contents index , -
.

(defconstant contents "contents")


(defconstant index "index")
(defun gen-contents (&optional (sections *sections*))
(page contents contents
(with ol
(dolist (s sections)
(link-item (section-id s) (section-title s))
(brs 2))
(link-item index (string-capitalize index)))))
(defun gen-index (&optional (sections *sections*))
(page index index
(with ol
(dolist (i (all-items sections))
(link-item (item-id i) (item-title i))
(brs 2)))))
(defun all-items (sections)
(let ((is nil))
(dolist (s sections)
(dolist (i (section-items s))
(setf is (merge list (list i) is #title<))))
is))
(defun title< (x y)
(string-lessp (item-title x) (item-title y)))

. 16.9.

gen-contents gen-index . -
HTML-, -
. ,
. all-items, -
title<. , -
272 16. : HTML

string-lessp, , string<, -
.
, -
, a the.
. 16.10: gen-site
-, .

(defun gen-site ()
(map3 #gen-section *sections*)
(gen-contents)
(gen-index))
(defun gen-section (sect <sect sect>)
(page (section-id sect) (section-title sect)
(with ol
(map3 #(lambda (item <item item>)
(link-item (item-id item)
(item-title item))
(brs 2)
(gen-item sect item <item item>))
(section-items sect)))
(brs 3)
(gen-move-buttons (if <sect (section-id <sect))
contents
(if sect> (section-id sect>)))))
(defun gen-item (sect item <item item>)
(page (item-id item) (item-title item)
(princ (item-text item))
(brs 3)
(gen-move-buttons (if <item (item-id <item))
(section-id sect)
(if item> (item-id item>)))))
(defun gen-move-buttons (back up forward)
(if back (button back "Back"))
(if up (button up "Up"))
(if forward (button forward "Forward")))

. 16.10. ,

, , -
, , , -
.
, . 16.9. -
gen-section, gen-item
, -
.
16.4. 273

:
, , -
; -
gen-move-buttons
, . :
gen-section, -
, gen-item .
.
, , HTML-.
.

. 16.11. -
.

(defitem des "Fortune Cookies: Dessert or Fraud?" "...")


(defitem case "The Case for Pessimism" "...")
(defsection position "Position Papers" des case)
(defitem luck "Distribution of Bad Luck" "...")
(defitem haz "Health Hazards of Optimism" "...")
(defsection abstract "Research Abstracts" luck haz)
(defsite position abstract)

. 16.11.
17
17. :


- . -
(embedded language). --
. -
, , -
- -
.

17.1.
11.10 ,
. :
1. .
2. .
3. .
CLOS, , .
,
CLOS, -
.
. -
-
. -
gethash:
(gethash color obj)
,
. , .
17.1. 275

,
funcall :
(funcall (gethash move obj) obj 10)
, -
Smalltalk:
(defun tell (obj message &rest args)
(apply (gethash message obj) obj args))
, 10, -
:
(tell obj move 10)
, , -
, .
gethash, -
. 17.1. ( rget recursive get, -
.)
.

(defun rget (prop obj)


(multiple-value-bind (val in) (gethash prop obj)
(if in
(values val in)
(let ((par (gethash :parent obj)))
(and par (rget prop par))))))
(defun tell (obj message &rest args)
(apply (rget message obj) obj args))

. 17.1.

. -
, ,
:
> (setf circle-class (make-hash-table)
our-circle (make-hash-table)
(gethash :parent our-circle) circle-class
(gethash radius our-circle) 2)
2
circle-class area .
, :
> (setf (gethash area circle-class)
#(lambda (x)
(* pi (expt (rget radius x) 2))))
#<Interpreted-Function BF1EF6>
276 17. :

our-circle.
: rget -
, tell :
> (rget radius our-circle)
2
T
> (tell our-circle area)
12.566370614359173
, -
, . -
CLOS - .
? ,
- : -
!
, , -
. , -
. ,
, ,
.

17.2.
,
. -
, parent -
rget, . 17.2.

(defun rget (prop obj)


(dolist (c (precedence obj))
(multiple-value-bind (val in) (gethash prop c)
(if in (return (values val in))))))
(defun precedence (obj)
(labels ((traverse (x)
(cons x
(mapcan #traverse
(gethash :parents x)))))
(delete-duplicates (traverse obj))))

. 17.2.

, -
, -
.
, , . .
17.2. 277


, -
, , -
. , ,
, . 17.3: a b c,
d. ( )
: a, b, d, c, d. d c,
d, c, ,
, .

b c

. 17.3.

, , -
.
: a, b, c, d. -
? -
, ,
.
precedence.
traverse, . -
, .
, -
CLOS .
( , , 3
. 192.) Common Lisp
delete-duplicates.
, . -
, rget -
.
, , ,

:1

1
(serves) (self) (country),
. . .
278 17. :

> (setf scoundrel (make-hash-table)


patriot (make-hash-table)
patriotic-scoundrel (make-hash-table)
(gethash serves scoundrel) self
(gethash serves patriot) country
(gethash :parents patriotic-scoundrel)
(list scoundrel patriot))
(#<Hash-Table C41C7E> #<Hash-Table C41F0E>)
> (rget serves patriotic-scoundrel)
SELF
T
, -
. -
, .

17.3.
-, , -
. -
, -
,
, -
,
.
, -
, -
.
, - -
, -
. , ,
, -
, .
, ;

.
. 17.4. . *objs* -
. parents -
; (setf parents) , -
make-precedence -
, . , ,
precedence.
make-hash-table -
obj,
. rget, -
.
17.4. 279

(defvar *objs* nil)


(defun parents (obj) (gethash :parents obj))
(defun (setf parents) (val obj)
(prog1 (setf (gethash :parents obj) val)
(make-precedence obj)))
(defun make-precedence (obj)
(setf (gethash ipreclist obj) (precedence obj))
(dolist (x *objs*)
(if (member obj (gethash :preclist x))
(setf (gethash :preclist x) (precedence x)))))
(defun obj (&rest parents)
(let ((obj (make-hash-table)))
(push obj *objs*)
(setf (parents obj) parents)
obj))
(defun rget (prop obj)
(dolist (c (gethash :preclist obj))
(multiple-value-bind (val in) (gethash prop c)
(if in (return (values val in))))))

. 17.4.

17.4.
. -
tell , -
:
(tell (tell obj find-owner) find-owner)
tell, .
defprop (. 17.5). -
meth?, , , -
. , -
, rget, .
, ,
(defprop find-owner t)
,
:
(find-owner (find-owner obj))
280 17. :

(defmacro defprop (name &optional meth?)


(progn
(defun ,name (obj &rest args)
,(if meth?
(run-methods obj ,name args)
(rget ,name obj)))
(defun (setf ,name) (val obj)
(setf (gethash ,name obj) val))))
(defun run-methods (obj name args)
(let ((meth (rget name obj)))
(if meth
(apply meth obj args)
(error "No ~A method for "A." name obj))))

. 17.5.

:
> (progn
(setf scoundrel (obj)
patriot (obj)
patriotic-scoundrel (obj scoundrel patriot))
(defprop serves)
(setf (serves scoundrel) self
(serves patriot) country)
(serves patriotic-scoundrel))
SELF
T

17.5.
:
(defprop area t)

(setf circle-class (obj))

(setf (aref circle-class)


#(lambda (c) (* pi (expt (radius c) 2))))
cdr , -
call-next-method.
, -
, :
(setf grumpy-circle (obj circle-class))

(setf (area grumpy-circle)


#(lambda (c)
(format t "How dare you stereotype me!~%")
17.6. 281

(funcall (some #(lambda (x) (gethash area x))


(cdr (gethash :preclist c)))
c)))
funcall call-next-method,
, .
defmeth . 17.6 -
, .

(defmacro defmeth (name obj parms &rest body)


(let ((gobj (gensym)))
(let ((,gobj ,obj))
(setf (gethash ,name ,gobj)
(labels ((next () (get-next ,gobj ,name)))
#(lambda ,parms ,@body))))))
(defun get-next (obj name)
(some #(lambda (x) (gethash name x))
(cdr (gethash :preclist obj))))

. 17.6.

defmeth setf, labels--


, next .
next-method-p (. 182), ,
, call-next-method.
:
(defmeth area circle-class (c)
(* pi (expt (radius c) 2)))

(defmeth area grumpy-circle (c)


(format t "How dare you stereotype me!~%")
(funcall (next) c))
, defmeth
. , -
next.

17.6.
, -
: . , -
, .
- -
. ,
,
, , . -
282 17. :

,
.
. 17.7 inst, . -
( -
), -
. , *objs*. -
:
(setf grumpy-circle (inst circle-class))
-
, rget get-next, -
.
. ,
, -
. (setf parents) -
.

(defun inst (parent)


(let ((obj (make-hash-table)))
(setf (gethash :parents obj) parent)
obj))
(defun rget (prop obj)
(let ((prec (gethash :preclist obj)))
(if prec
(dolist (c prec)
(multiple-value-bind (val in) (gethash prop c)
(if in (return (values val in)))))
(multiple-value-bind (val in) (gethash prop obj)
(if in
(values val in)
(rget prop (gethash :parents obj)))))))
(defun get-next (obj name)
(let ((prec (gethash :preclist obj)))
(if prec
(some #(lambda (x) (gethash name x))
(cdr prec))
(get-next (gethash obj :parents) name))))

. 17.7.

17.7.
-
. --
.
17.7. 283

. -,
, -
. , -
.
, -
,
. - -
. -
,
.
, , -
. , -
, .
-
, -
.
. 17.817.10 . -
17.8 -
. .
, ,
. 17.8 :
1. parents :parents
-.
, .
2. layout () ,
-
, 1.
3. preclist :preclist -
-. -
, nil.
, -
setf (. 10.6).
class . -
,
. , . -
, -
.
> (setf *print-array* nil
geom-class (class nil area)
circle-class (class (geom-class) radius))
#<Simple-Vector T 5 C6205E>

1
3 . ,
layout . . .
284 17. :

(defmacro parents (v) (svref ,v 0))


(defmacro layout (v) (the simple-vector (svref ,v 1)))
(defmacro preclist (v) (svref ,v 2))
(defmacro class (&optional parents &rest props)
(class-fn (list ,@parents) ,props))
(defun class-fn (parents props)
(let* ((all (union (inherit-props parents) props))
(obj (make-array (+ (length all) 3)
:initial-element :nil)))
(setf (parents obj) parents
(layout obj) (coerce all simple-vector)
(preclist obj) (precedence obj))
obj))
(defun inherit-props (classes)
(delete-duplicates
(mapcan #(lambda (c)
(nconc (coerce (layout c) list)
(inherit-props (parents c))))
classes)))
(defun precedence (obj)
(labels ((traverse (x)
(cons x
(mapcan #traverse (parents x)))))
(delete-duplicates (traverse obj))))
(defun inst (parent)
(let ((obj (copy-seq parent)))
(setf (parents obj) parent
(preclist obj) nil)
(fill obj :nil :start 3)
obj))

. 17.8. :

: geom-class -
area; circle-class
geom-class radius.1 layout -
:2
> (coerce (layout circle-class) list)
(AREA RADIUS)

1
*print-array* nil.
preclist ,
.
2
. *print-
array* nil, .
17.7. 285

class class-fn, -
. inherit-props
, -
. (preclist -
precedence, .) -
:nil, ,
. area circle-class,
:
> (svref circle-class
(+ (position area (layout circle-class)) 3))
:NIL
, -
.
, inst .
, :
> (setf our-circle (inst circle-class))
#<Simple-Vector T 5 C6464E>
inst class-fn, .
, -
, .
. , -
,
. ,
. , , , -
, .
, , -
. . 17.9
rget. rget . 17.7.
.
1. , -
, , -
, :nil. , -
:nil.
2. , ,
, , rget.
rget next?, -
. , nil,
rget .
lookup gethash rget.

. svref:
> (lookup area circle-class)
:NIL
286 17. :

(declaim (inline lookup (setf lookup)))


(defun rget (prop obj next?)
(let ((prec (preclist obj)))
(if prec
(dolist (c (if next? (cdr prec) prec) :nil)
(let ((val (lookup prop c)))
(unless (eq val :nil) (return val))))
(let ((val (lookup prop obj)))
(if (eq val :nil)
(rget prop (parents obj) nil)
val)))))
(defun lookup (prop obj)
(let ((off (position prop (layout obj) :test #eq)))
(if off (svref obj (+ off 3)) :nil)))
(defun (setf lookup) (val prop obj)
(let ((off (position prop (layout obj) :test #eq)))
(if off
(setf (svref obj (+ off 3)) val)
(error "Cant set ~A of ~A." val obj))))

. 17.9. :

setf lookup , -
area circle-class:
(setf (lookup area circle-class)
#(lambda (c)
(* pi (expt (rget radius c nil) 2))))
, , -
,
. .
17.10 .1
, -
. defprop, ,
, lookup gethash. ,
:
> (defprop radius)
(SETF RADIUS)
> (radius our-circle)
:NIL

1
,
(Tim Menzies)
comp.lang.lisp (19.12.2010). -
. . .
17.7. 287

> (setf (radius our-circle) 2)


2
defprop , -
run-methods,
.
, defmeth
. :
defprop, lookup gethash rget get-next
(. 282) . -
rget: -
get-next,
. , rget -
get-next.

(declaim (inline run-methods))


(defmacro defprop (name &optional meth?)
(progn
(defun ,name (obj &rest args)
,(if meth?
(run-methods obj ,name args)
(rget ,name obj nil)))
(defun (setf ,name) (val obj)
(setf (lookup ,name obj) val))))
(defun run-methods (obj name args)
(let ((meth (rget name obj nil)))
(if (not (eq meth :nil))
(apply meth obj args)
(error "No ~A method for ~A." name obj))))
(defmacro defmeth (name obj parms &rest body)
(let ((gobj (gensym)))
(let ((,gobj ,obj))
(defprop ,name t)
(setf (lookup ,name ,gobj)
(labels ((next () (rget ,name ,gobj t)))
#(lambda ,parms ,@body))))))

. 17.10. :

, -
, :
(defmeth area circle-class (c)
(* pi (expt (radius c) 2)))
, rget
radius, defprop.
288 17. :

defprop defmeth -
area, our-circle:
> (area our-circle)
12.566370614359173

17.8.
,
- . ,
. , -
, -
, . -
-
.
-
.
, , . -
, -
. -
,
. -
.
, , , , -
. -
, ,
, ,
13.4.
-
. , , .
, -
, ,
, .

( 1980- -
, ), , -
. -
:

-
; .

, , , -
:
17.8. 289

1. , -
; .
2. -, -
.
,
.
-
. ,
CLOS ( , ,
call-next-method). -
CLOS 70- .
, -
, . -, ,
.
, --
, -
CLOS.
CLOS , -
.
, -
. CLOS
, .
,
, -
. CLOS
, CLOS .
CLOS:
, -
. , -
, , . -
,
. -
, .
: ?
A
A.

, ,
.


, ,
, ,
(break loop). -
, , , -
: , -
.
- -
, toplevel. , -
,
. -
, ,
. , , -
, -
toplevel. -
:abort:
> (/ 1 0)
Error: Division by zero.
Options: :abort, :backtrace
>> :abort
>
, .
291

-
, .1 -
, ,
:
>> (/ 2 0)
Error: Division by zero.
Options: :abort, :backtrace, :previous
>>>
, -
toplevel.


, , -
, . (trace foo),
, foo
, -
. -
.
.
, , , ,
,
(defun tree1+ (tr)
(cond ((null tr) nil)
((atom tr) (1+ tr))
(t (cons (tree1+ (car tr))
(tree1+ (cdr tr))))))
, :
> (trace treel+)
(treel+)
> (treel+ ((1 . 3) 5 . 7))
1 Enter TREE1+ ((1 . 3) 5 . 7)
2 Enter TREE1+ ( 1 . 3 )
3 Enter TREE1+ 1
3 Exit TREE1+ 2
3 Enter TREE1+ 3
3 Exit TREE1+ 4
2 Exit TREE1+ (2 . 4)
2 Enter TREE1+ (5 . 7)
3 Enter TREE1+ 5
3 Exit TREE1+ 6
3 Enter TREE1+ 7
3 Exit TREE1+ 8

1
break loop. . .
292 A

2 Exit TREE1+ (6 . 8)
1 Exit TREE1+ ((2 . 4) 6 . 8)
((2 . 4) 6 . 8)
foo (untrace foo);
(untrace).

. , , ,
, .
, -
.
(backtrace) ,
, , -
. : ,
, :
?. . -
,

( toplevel ).
, :backtrace
:
> (tree1+ ((1 . 3) 5 . A))
Error: A is not a valid argument to 1+.
Options: :abort, :backtrace
>> :backtrace
(1+ A)
(TREE1+ A)
(TREE1+ (5 . A))
(TREE1+ ((1 . 3) 5 . A))

. .
( 2.12) -
, . -
,
, .
, ,
.
, -
. , -
( ) -
, .
,
, . -
-
. :
Common Lisp .
293


. -
,
. , . -
,
.
( ),
,
:
> (defun blow-stack () (1+ (blow-stack)))
BLOW-STACK
> (blow-stack)
Error: Stack overflow.
, -
, .
, ,
. -
, , , -
.
-
. ,
, obj lst,
lst. , .
: obj lst,
lst . -
.
Common Lisp car cdr nil,
nil:
> (car nil)
NIL
> (cdr nil)
NIL
member,
(defun our-member (obj lst) ; wrong
(if (eql (car lst) obj)
lst
(our-member obj (cdr lst))))
, -
. ,
, :
(our-member obj nil)
294 A

(. 33) -
, nil.
car cdr nil, nil, -
.
, ,
. .
,
. , our-member ,
.
, -
. , -
(. 215)
. , -
, -
. -
(. 205): -
, .
,
. Enter -
- , .
,
. : -
, -
, ,
:
> (format t "for example ~A~% this)
-
. Enter -, -
, .
-
. ,
,

.

/

-
. -
.
, let defun,
, -
295

. - -
:
> (progn
(let ((x 10))
(format t "Here x = ~A.~%" x))
(format t "But now its gone...~%")
x)
Here x = 10.
But now its gone...
Error: X has no value.
, ,
, . -
x, ,

. , .
.
, -
:
> defun foo (x) (+ x 1))
Error: DEFUN has no value.
: defun ?
, -
defun ( , ) .
-
. defvar, -
, ,
.

nil
, nil ,
, - -
. nil -
. , nil , -
, -
-
.
, , ,
, :
(defun month-length (mon)
(case mon
((jan mar may jul aug dec) 31)
((apr jun sept nov) 30)
(feb (if (leap-year) 29 28))))
296 A

,
:
(defun month-weeks (mon) (/ (month-length mon) 7.0))
month-weeks :
> (month-weeks oct)
Error: NIL is not a valid argument to /.
case- ,
case nil. month-weeks, ,
/, nil, .
.
, .
-
cond case
. Common Lisp ecase (.
14.6).


, . -
, , (-
) :
(defun depth (x)
(if (atom x)
1
(1+ (apply #max (mapcar #depth x)))))
, , -
1:
> (depth ((a)))
3
0, 1. ,
:
(defun nesting-depth (x)
(if (atom x)
0
(1+ (apply #max (mapcar #depth x)))))
:
> (nesting-depth ((a)))
3
. ? , -
, . -
297

, ,
-
depth.




,
( ) -
. , read-from-string -
:
(read-from-string string &optional eof-error eof-value
&key start end preserve-whitespace)
,
. ,
,
> (read-from-string "abcd" :start 2)
ABCD
4
:start 2 . -
:
> (read-from-string "abcd" nil nil :start 2)
CD
4


13 , -
. -,
,
. ,
double-float,
(defun df* (a b)
(declare (double-float a b))
(* a b))
, -
double-float.
df* , -
, . -

:
298 A

> (df* 2 3)
Error: Interrupt.
, -
.

- -
. .
, , -
, -
. , map-int (. 117)
x . ,
, ignore:
(map-int #(lambda (x)
(declare (ignore x))
(random 100))
10)
B
B.

58 -
Common Lisp. ( -
) -
( ), .
, , Com-
mon Lisp , . -
Common Lisp , , -
. , ,
. ,
, :
apply aref backquote block car cdr ceiling char= cons defmacro
documentation eq error expt fdefinition function floor gensym
get-setf-expansion if imagpart labels length multiple-value-bind
nth-value quote realpart symbol-function tagbody type-of typep = + - / < >
, , ,
Common Lisp, .
-
. -
.
,
, .
(defun -abs (n)
(if (typep n complex)
(sqrt (+ (expt (realpart n) 2) (expt (imagpart n) 2)))
(if (< n 0) (- n) n)))

(defun -adjoin (obj lst &rest args)


(if (apply #member obj lst args) lst (cons obj lst)))
300 B

(defmacro -and (&rest args)


(cond ((null args) t)
((cdr args) (if ,(car args) (-and ,@(cdr args))))
(t (car args))))

(defun -append (&optional first &rest rest)


(if (null rest)
first
(nconc (copy-list first) (apply #-append rest))))

(defun -atom (x) (not (consp x)))

(defun -butlast (lst &optional (n 1))


(nreverse (nthcdr n (reverse lst))))

(defun -cadr (x) (car (cdr x)))

(defmacro -case (arg &rest clauses)


(let ((g (gensym)))
(let ((,g ,arg))
(cond ,@(mapcar #(lambda (cl)
(let ((k (car cl)))
(,(cond ((member k (t otherwise))
t)
((consp k)
(member ,g ,k))
(t (eql ,g ,k)))
(progn ,@(cdr cl)))))
clauses)))))

(defun -cddr (x) (cdr (cdr x)))

(defun -complement (fn)


#(lambda (&rest args) (not (apply fn args))))

(defmacro -cond (&rest args)


(if (null args)
nil
(let ((clause (car args)))
(if (cdr clause)
(if ,(car clause)
(progn ,@(cdr clause))
(-cond ,@(cdr args)))
(or ,(car clause)
(-cond ,@(cdr args)))))))

(defun -consp (x) (typep x cons))

(defun -constantly (x) #(lambda (&rest args) x))


301

(defun -copy-list (lst)


(labels ((cl (x)
(if (atom x)
x
(cons (car x)
(cl (cdr x))))))
(cons (car lst)
(cl (cdr lst)))))

(defun -copy-tree (tr)


(if (atom tr)
tr
(cons (-copy-tree (car tr))
(-copy-tree (cdr tr)))))

(defmacro -defun (name parms &rest body)


(multiple-value-bind (dec doc bod) (analyze-body body)
(progn
(setf (fdefinition ,name)
#(lambda ,parms
,@dec
(block ,(if (atom name) name (second name))
,@bod))
(documentation ,name function)
,doc)
,name)))

(defun analyze-body (body &optional dec doc)


(let ((expr (car body)))
(cond ((and (consp expr) (eq (car expr) declare))
(analyze-body (cdr body) (cons expr dec) doc))
((and (stringp expr) (not doc) (cdr body))
(if dec
(values dec expr (cdr body))
(analyze-body (cdr body) dec expr)))
(t (values dec doc body)))))

; , , ; let.

(defmacro -do (binds (test &rest result) &rest body)


(let ((fn (gensym)))
(block nil
(labels ((,fn ,(mapcar #car binds)
(cond (,test ,@result)
(t (tagbody ,@body)
(,fn ,@(mapcar #third binds))))))
(,fn ,@(mapcar #second binds))))))
302 B

(defmacro -dolist ((var lst &optional result) &rest body)


(let ((g (gensym)))
(do ((,g ,lst (cdr ,g)))
((atom ,g) (let ((,var nil)) ,result))
(let ((,var (car ,g)))
,@body))))

(defun -eql (x y)
(typecase x
(character (and (typep y character) (char= x y)))
(number (and (eq (type-of x) (type-of y))
(= x y)))
(t (eq x y))))

(defun -evenp (x)


(typecase x
(integer (= 0 (mod x 2)))
(t (error "non-integer argument"))))

(defun -funcall (fn &rest args) (apply fn args))

(defun -identity (x) x)

; :
; (let ((&key 1) (&optional 2))) , .

(defmacro -let (parms &rest body)


((lambda ,(mapcar #(lambda (x)
(if (atom x) x (car x)))
parms)
,@body)
,@(mapcar #(lambda (x)
(if (atom x) nil (cadr x)))
parms)))

(defun -list (&rest elts) (copy-list elts))

(defun -listp (x) (or (consp x) (null x)))

(defun -mapcan (fn &rest lsts)


(apply #nconc (apply #mapcar fn lsts)))

(defun -mapcar (fn &rest lsts)


(cond ((member nil lsts) nil)
((null (cdr lsts))
(let ((lst (car lsts)))
(cons (funcall fn (car lst))
(-mapcar fn (cdr lst)))))
(t
(cons (apply fn (-mapcar #car lsts))
(apply #-mapcar fn
(-mapcar #cdr lsts))))))
303

(defun -member (x lst &key test test-not key)


(let ((fn (or test
(if test-not
(complement test-not))
#eql)))
(member-if #(lambda (y)
(funcall fn x y))
lst
:key key)))

(defun -member-if (fn lst &key (key #identity))


(cond ((atom lst) nil)
((funcall fn (funcall key (car lst))) lst)
(t (-member-if fn (cdr lst) :key key))))

(defun -mod (n m)
(nth-value 1 (floor n m)))

(defun -nconc (&optional lst &rest rest)


(if rest
(let ((rest-conc (apply #-nconc rest)))
(if (consp lst)
(progn (setf (cdr (last lst)) rest-conc)
lst)
rest-conc))
lst))

(defun -not (x) (eq x nil))

(defun -nreverse (seq)


(labels ((nrl (lst)
(let ((prev nil))
(do ()
((null lst) prev)
(psetf (cdr lst) prev
prev lst
lst (cdr lst)))))
(nrv (vec)
(let* ((len (length vec))
(ilimit (truncate (/ len 2))))
(do ((i 0 (1+ i))
(j (1- len) (1- j)))
((>= i ilimit) vec)
(rotatef (aref vec i) (aref vec j))))))
(if (typep seq vector)
(nrv seq)
(nrl seq))))

(defun -null (x) (eq x nil))


304 B

(defmacro -or (&optional first &rest rest)


(if (null rest)
first
(let ((g (gensym)))
(let ((,g ,first))
(if ,g
,g
(-or ,@rest))))))

; Common Lisp,
; .

(defun pair (lst)


(if (null lst)
nil
(cons (cons (car lst) (cadr lst))
(pair (cddr lst)))))

(defun -pairlis (keys vals &optional alist)


(unless (= (length keys) (length vals))
(error "mismatched lengths"))
(nconc (mapcar #cons keys vals) alist))

(defmacro -pop (place)


(multiple-value-bind (vars forms var set access)
(get-setf-expansion place)
(let ((g (gensym)))
(let* (,@(mapcar #list vars forms)
(,g ,access)
(,(car var) (cdr ,g)))
(prog1 (car ,g)
,set)))))

(defmacro -prog1 (arg1 &rest args)


(let ((g (gensym)))
(let ((,g ,arg1))
,@args
,g)))

(defmacro -prog2 (arg1 arg2 &rest args)


(let ((g (gensym)))
(let ((,g (progn ,arg1 ,arg2)))
,@args
,g)))

(defmacro -progn (&rest args) (let nil ,@args))


305

(defmacro -psetf (&rest args)


(unless (evenp (length args))
(error "odd number of arguments"))
(let* ((pairs (pair args))
(syms (mapcar #(lambda (x) (gensym))
pairs)))
(let ,(mapcar #list
syms
(mapcar #cdr pairs))
(setf ,@(mapcan #list
(mapcar #car pairs)
syms)))))

(defmacro -push (obj place)


(multiple-value-bind (vars forms var set access)
(get-setf-expansion place)
(let ((g (gensym)))
(let* ((,g ,obj)
,@(mapcar #list vars forms)
(,(car var) (cons ,g ,access)))
,set))))

(defun -rem (n m)
(nth-value 1 (truncate n m)))

(defmacro -rotatef (&rest args)


(psetf ,@(mapcan #list
args
(append (cdr args)
(list (car args))))))

(defun -second (x) (cadr x))

(defmacro -setf (&rest args)


(if (null args)
nil
(setf2 ,@args)))

(defmacro setf2 (place val &rest args)


(multiple-value-bind (vars forms var set)
(get-setf-expansion place)
(progn
(let* (,@(mapcar #list vars forms)
(,(car var) ,val))
,set)
,@(if args ((setf2 ,@args)) nil))))

(defun -signum (n)


(if (zerop n) 0 (/ n (abs n))))
306 B

(defun -stringp (x) (typep x string))

(defun -tailp (x y)
(or (eql x y)
(and (consp y) (-tailp x (cdr y)))))

(defun -third (x) (car (cdr (cdr x))))

(defun -truncate (n &optional (d 1))


(if (> n 0) (floor n d) (ceiling n d)))

(defmacro -typecase (arg &rest clauses)


(let ((g (gensym)))
(let ((,g ,arg))
(cond ,@(mapcar #(lambda (cl)
((typep ,g ,(car cl))
(progn ,@(cdr cl))))
clauses)))))

(defmacro -unless (arg &rest body)


(if (not ,arg)
(progn ,@body)))

(defmacro -when (arg &rest body)


(if ,arg (progn ,@body)))

(defun -1+ (x) (+ x 1))

(defun -1- (x) (- x 1))

(defun ->= (first &rest rest)


(or (null rest)
(and (or (> first (car rest)) (= first (car rest)))
(apply #->= rest))))
C
C. Common Lisp

ANSI Common Lisp Common Lisp, -


1984 Common Lisp: the Language -
. ( )
1990 . -
. , 1990 , -
.


1. Common Lisp (CLOS) .
2. loop -
.
3. Common Lisp , -
-
.
4. Common Lisp -
(pretty printing).


1. :
complement nth-value
declaim print-unreadable-object
defpackage readtable-case
delete-package row-major-aref
destructuring-bind stream-external-format
fdefinition with-compilation-unit
file-string-length with-hash-table-iterator
308 C

function-lambda-expression with-package-iterator
load-time-value with-standard-io-syntax
map-into
2. :
*debugger-hook* *read-eval* *print-readably*

1.
(setf f).
, .
fdefinition symbol-function, -
.
2. function fboundp- --
. ( -) - -
, . -
coerce.
3. , ,
. ( , -
keyword -
.)
4. (&rest) -
, .
5. , flet labels,
defmacro, macrolet defsetf -
block,
.
6. ,
( toplevel defun
let) .

1. -,
.
2. ,
defmacro. ANSI Common Lisp
:
(let ((op car))
(defmacro pseudocar (lst)
(,op ,lst)))
1984
( toplevel).
Common Lisp 309

3. , -
.
4. .
5. declare.


1. eval-when, -
.
2. compile-file :print :verbose.
-
*compile-print* *compile-verbose*.
3. *compile-file-pathname* *compile-file-truename*
compile-file, *load-pathname*
*load-truename* load.
4. dynamic-extent.
5. debug.
6. compiler-let.
7. #,.
8. *break-on-warnings*; -
*break-on-signals*.


1. setf .
2. -
. , , -
,
, . -
,
.
3.
( dolist) -
, .

1. *gensym-counter* -
, , -
gensym. 1984 -
gensym;
.
310 C

2. , , -
.
3. documentation .

1. assoc-if, assoc-if-not, rassoc-if, rassoc-if-not reduce


:key.
2. last , -
.
3. complement -if-not -
:test-not .

1. ,
(type-upgrading) -
.
2. fixnum.


1. string-char. string (vector
string-char). character : base-char
extended-char. string base-string, -
, , simple-base-string.
2. -
:element-type.
3. font bits, -
. , -
Common Lisp, code.
4. -
. , (string= x x) t.

1. -
defstruct.
2. , -
defstruct , .
Common Lisp 311

-
1. equalp -.
2. , -
-: hash-table-rehash-size, hash-table-
rehash-threshold, hash-table-size hash-table-test.

-
1. ,
.
2. ,
.
3. ~_, ~W ~I. ~D, ~B, ~O,
~X ~R .
4. write write-to-string
.
5. #P.
6. *print-readably* -
.

1. fixnum 16 .
2. ,
.
3. real; number
rational float.

1. -
. , , top-
level .
defpackage. -
in-package .
, :use -
.
2. lisp user common-lisp common-lisp-user.
common-lisp ,
lisp.
3. , ,
common-lisp. , -
312 C

, -
.

1. eql.
2. common commonp.

1990
1. :
allocate-instance ensure-directories-exist
array-displacement lambda
constantly read-sequence
define-symbol-macro write-sequence
2. :
applyhook function-information
*applyhook* get-setf-method
augment-environment generic-flet
declare generic-function
enclose generic-labels
evalhook parse-macro
*evalhook* variable-information
declaration-information with-added-methods
define-declaration
get-setf-method get-setf-expansion, -
get-setf-method-multiple-value. ,
declare, .
3. :
define-setf-[method expander]
get-setf-[method-multiple-value expansion]
special-[form operator]-p
simple-condition-format-[string control]
( 1990 ):
base-[character char]
extended-[character char]
4. , 1990 , ,
.
5. setf values-
.
6. ANSI ,
. , , -
Common Lisp 313

nconc. (, -
append , nconc
append .)
7. integer character
coerce. (setf f)
coerce.
8. , compile
; -
-
-. -
.
9. gentemp set .
10. type . -
, ,
, .
11. ignorable , -
, , -
.
12. array-total-size-limit fixnum. -
row-major-aref fixnum.
13. :print-function , defstruct, -
:print-object, -
.
14. boolean, nil t.
D
D.

ANSI Common Lisp.


:

,
, . -
-
, .
1, ,
, : (a*) -
(), (a), (a a) . .
: (a [b] c) (a c) (a b c).
: ({a b}*) -
(), (a b), (a b a b) . . -
: (a {1 | 2} b)
(a 1 b) (a 2 b).


.
, , -
. -
( ) .

, .
,

1
* *.
315

, , -
. ,
, .

alist , . . -
, (key . value).
body ,
defun: declaration* [string] expres-
sion*, [string] declaration* expression*.
(defun fname parameters . body) , -
defun (defun fname parameters declaration*
[string] expression*) (defun fname parameters [string]
declaration* expression*). string
expression, .
c .
declaration , car declare.
environment , -
. ( ,
.) nil
.
f .
fname : , (setf s), s .
format , format,
, -
() - .
i .
list . ,
. , -
, cdr-
,
. , nth cdr-
, find . , list,
car-.
n .
object .
package , , , ,
.
path , , ( -
-
), .
place , setf.
plist , . .
.
pprint- (
dispatch nil).
predicate .
316 D

prolist .
proseq , . .
.
r (real).
tree , ; car- cdr-
.
type .

-
. stream -
*standard-input* *standard-output* -
. package -
*package*; readtable *readtable*,
pprint-dispatch *print-pprint-dispatch*.

, , -
: key, test, test-not, from-end,
start end. (. 79).
key, test test-not , start end -
. -
, -
.
, , -
eql.

(, ),
, -
, , , -
,
. -
, (list).
, , -
.


(compile fname &optional function)
function fname -
, compile
317

, fname. (-
-
, -
, .) function ( -
-) , compile
, fname,
. fname nil, -
. -
: , -
; -
, -
, (style warnings).
(compiler-macro-function fname &optional environment)
,
environment fname ( nil, ). -
: -
, .
setf.
(constantp expression &optional environment)
, expression ,
, car quote, , -
, cons-.
, -
.
(declaim declaration-spec)
proclaim, toplevel -
, declaration-spec .
(declare declaration-spec*)
, -
. , car declare,
. declaration-spec
( ) , -
. : dynamic-extent, ftype,
ignorable, ignore, inline, notinline, optimize, special, type.
(define-compiler-macro fname parameters . body)
defmacro, . -
, -
. fname
.
,
. ( -
, .) -
compiler-macro fname.
318 D

(define-symbol-macro symbol expression)


symbol ,
symbol.
expression. symbol
setq, setq setf; multiple-
value-setq setf values.
(defmacro symbol parameters . body)
symbol. -
, (symbol a1an) -
, ((lambda parameters .
body) a1an). parameters -
, destructing-bind; -
&whole, -
, &environment, , -
. -
function symbol.
(eval expression)

().
(eval-when (case*) expression*)
case, expressions
; -
nil. (case) :compile-toplevel , -
eval-when
. :load-toplevel ,
eval-when toplevel . :execute -
, eval-when
( :execute -
eval-then progn). compile, load eval -
:compile-toplevel, :load-toplevel
:execute.
(lambda parameters . body)
(function (lambda parameters . body)).
(load-time-value expression
&optional constant)
(quote val), val , -
expression , -
load-time-value. constant ( ) -
t, , .
(locally declaration* expression*)
(locally e1en) (let () e1en), -
, ;
319

expressions -
.
(macroexpand expression &optional environment)
expression, -
macroexpand-1 ,
. -
, expression.
(macroexpand-1 expression &optional environment)
expression ,
, expression. -
: *macroexpand-hook*, fun-
call, : , expres-
sion environment. ,
macroexpand-1 .
, -
expression.
(macro-function symbol &optional environment)
symbol environ-
ment nil .
: , -
. setf.
(proclaim declaration-spec)
declaration-spec. -
: declaration, ftype, inline, notinline,
optimize, special, type.
(special-operator-p symbol)
, symbol -
.
(symbol-macrolet ((symbol expression)*)
declaration* expression*)
, symbol -
-, , -
define-symbol-macro.
(the type expression)
expression -
type. , -
. ( values -
. 418.) -
: t nil;
.
320 D

(quote object)
.


(coerce object type)
type. object
type, . -
, -
type , object, -
type ,
object. ,
, character,
.
(real) type ,
object
. (, (setf f),
-) function, ,
; -
,
coerce.
(deftype name parameters . body)
defmacro, , name -
(, declare), -
. type
name.
(subtypep type1 type2 &optional environment)
: ,
, type1 type2, -
.
(type-error-datum condition)
, condition.
(type-error-expected-type condition)
, ,
condition.
(type-of object)
,
object.
(typep object type &optional environment)
, object type.
321


(and expression*)
. -
nil, nil.
, .
, t.
(apply function &rest args)
function args,
. arg .
function args, -
, args, -
, list*.
function ,
.
(block symbol expression*)
, symbol ( -
). return-from.
(case object (key expression*)* [({t | otherwise} expression*)])
, -
: (eql) key (
) (member) , -
-
. t otherwise . -
, nil. t otherwise
,
, (t) (otherwise).
(catch tag expression*)
, ,
tag. throw.
(ccase object (key expression*)*)
case, , -
. , -
.
(compiled-function-p object)
, object -
.
(complement predicate)
, -
, predicate (
322 D

) , , , pre-
dicate .
(cond (test expression*)*)
test ,
. - , -
,
-
. ,
nil.
(constantly object)
, -
; object.
(ctypecase object (type expression*)*)
, , -
type, ( ). -
,
expressions () .
nil. -
.
(defconstant symbol expression [string])
-
expression.
. expression -
. string, , -
variable symbol. symbol.
(define-modify-macro name parameters symbol [string])
(define-modify-macro m (p1pn) f)
m, (m place a1an)
place (f val a1an), val pla-
ce. -
. string, ,
.
(define-setf-expander reader parameters . body)
(setf (reader a1an)
val). get-setf-expansion -
. string, , -
setf reader.
(defparameter symbol expression [string])
symbol
expression. string, ,
variable symbol. symbol.
323

(defsetf reader writer [string])


: (setf (reader a1an) val)
(writer a1an val); reader writer -
. string, ,
setf reader.
(defsetf reader parameters (var*) . body)
: (setf (reader a1an)
val) , defsetf,
defmacro. reader ( ),
,
; parameters reader,
vars () val. string, -
, setf reader.
, setf -
, defsetf
.
(defun fname parameters . body)
fname , -
, defun.
fname, fname , f,
fname (setf f). string, ,
function fname.
(defvar symbol [expression [string]])
symbol expression, -
expression -
. string, , vari-
able symbol. symbol.
(destructing-bind variables tree declaration* expression*)
variables ( -
, ), -
, tree. -
.
(ecase object (key expression*)*)
ccase, -
.
(eq object1 object2)
, object1 object2 .
(eql object1 object2)
, object1 object2 eq
,
.
324 D

(equal object1 object2)


, object1 object2 eql;
cons-, car cdr
equal; -
( ), -
eql; ,
. -
.
(equalp object1 object2)
, object1 object2 equal,
char-equal =; cons-, car cdr -
equalp; -
,
equalp; ,
equalp; - -
,
,
equalp. , -
.
(etypecase object (key exression*)*)
ctypecase, , -
key.
(every predicate proseq &rest proseqs)
, predicate,
, -
, pro-
seqs, , n- ,
n proseq. -
, nil.
nil.
(fboundp fname)
, fname -
.
(fdefinition fname)
fname. -
setf.
(flet ((fname parameters . body)*)
declaration* expression*)
, fname -
. labels, -
;
( ).
325

(fmakunbound fname)
fname.
, . fname.
(funcall function &rest args)
function args.
,
, .
(function name)
name, -
, (setf f) -. f -
, (setf f)
.
(function-lambda-expression function)
-, -
function, nil. -
: , nil, ,
;
,
function.
(functionp object)
, object .
(labels ((fname parameters . body)*)
declaration* expression*)
, fname
.
flet, -
. (
).
(get-setf-expansion place &optional environment)
(v1v5),
(setf place val) environment. -
:
(gensym); ,
; , -
place; , -
,
v1 v3; , -
place v1.
(go tag)
tagbody,
, -
eql.
326 D

(identity object)
object.
(if test then [else])
test. , -
then; -
else nil else.
(let ({symbol | (symbol [value])}*)
declaration* expression*)
, -
nil value.
(let* ({symbol | (symbol [value])}*)
declaration* expression*)
let, , value
symbol.
(macrolet ((symbol parameters . body)*)
declaration* expression*)
, -
. -
, macrolet.
flet, .
(multiple-value-bind (symbol*) expression1
declaration* expression*)
expression1, ,
symbol ( )
expression1. ,
, nil; -
, , .
(multiple-value-call function expression*)
function () , -
expression.
(multiple-value-list expression)
, expression.
(multiple-value-prog1 expression1 expression*)
, ()
.
(multiple-value-setq (symbol*) expression)
( ) , -
expression. , -
nil; ,
.
327

(not object)
, object nil.
(notany predicate proseq &rest proseqs)
(notany predicate s1sn) (not (some pre-
dicate s1sn)).
(notevery predicate proseq &rest proseqs)
(notevery predicate s1sn) (not (every
predicate s1sn)).
(nth-value n expression)
n- () expression. -
0.
n+1 , nil.
(or expression*)
, -
. -
, nil.
, .
(prog ({symbol | (symbol [value])}*)
declaration* {tag | expression}*)
, -
value ( nil,
). tagbody nil, -
expressions go, return return-from.
(prog* ({symbol | (symbol [value])}*)
declaration* {tag | expression}*)
prog, value -
.
(prog1 expression1 expression*)
,
.
(prog2 expression1 expression2 expression*)
, -
.
(progn expression*)
, ()
.
(progv symbols values expression*)
,
symbols
328 D

values. , -
; , -
.
(psetf {place value}*)
setf, value
place, -
. , (psetf x y y x) x y.
(psetq {symbol value}*)
psetf, , .
(return expression)
(return-from nil expression). (
do) nil.
(return-from symbol expression)
() expression -
symbol ( ). -
return-from
.
(rotatef place*)
, -
. , ,
(rotatef a1an), a2 a1,
a3 a2 . ., a1 an. nil.
(setf {place value}*)
setq: value
. value
places, . -
value.
place : ;
, -
place;
apply : #aref, #bit
#sbit; ;
the values, () -
places; , setf-;
, - .
(setq {symbol value}*)
symbol -
value.
, .
value.
329

(shiftf place1 place* expression)


. -
; , (shiftf a1an
val), a2 a1 . ., an -
val. a1.
(some predicate proseq &rest proseqs)
, predicate,
, -
proseqs, ,
, , n-,
n proseq. -
, -
, .
(tagbody {tag | expression*})
expressions nil.
go, -
. tags,
, . , -
, . ,
do, prog prog*,
tagbody.
(throw tag expression)
() expression -
catch
(eq), tag.
(typecase object (type expression*)*)
[({t | otherwise} expression*)])
object, -
type ( ). -
t otherwise, -
.
nil, -
.
(unless test expression*)
(unless test e1en) (when (not test) e1
en).
(unwind-protect expression1 expression*)
()
. , -
.
330 D

(values &rest objects)


.
(values-list prolist)
prolist.
(when test expression*)
test. ,
expressions () ; -
nil. nil, -
expression.

(do ({var | (var [init [update]])}*)


(test result*)
declaration* {tag | expression}*)

var ( init ,
nil), , -
update ( , update -
). test; test
, , test , -
result , -
. tagbody,
do nil.
init ,
var, ,
do. var -
update, -
. , , -
let, , psetq.
(do* ({var | (var [init [update]])}*)
(test result*)
declaration* {tag | expression*})
do, -
, let*, ,
setq.
(dolist (var list [result])
declaration* {tag | expression}*)
, var
list. ,
. tagbody,
dolist nil. ()
result; , nil.
331

result var,
nil.
(dotimes (var integer [result])
declaration* {tag | expression}*)
, var
0 (- integer 1) .
integer , -
. tagbody, dotimes -
nil. () -
result nil, . re-
sult var,
.
(loop expression*)
: expression -
loop,
. -
nil.
(loop [name-clause] var-clause* body-clause*)
: loop- , -
:
1. -
, , .
2. loop-.
3. .
4. .
5. , -
, -
.
6. ,
.
-
.
, .
loop .
nil, named.
loop-
nil, . -
, -
collect, append, nconc, count, sum, maximize, minimize, always,
never thereis.
name-clause named; var-
clause with, initially, finally for;
332 D

body-clause ,
named, with for. .
: , -
. type -
;
,
.
: loop -
. -
: upfrom, from; downfrom, from; upto, to; downto, to;
the, each; of, in; when, if; hash-keys, hash-key; hash-value, hash-values; sym-
bol, symbols; present-symbol, present-symbols; external-symbol, external-
symbols; do, doing; collect, collecting; append, appending; nconc, nconcing;
count, counting; sum, summing; maximize, maximizing; minimize, minimizing.
named symbol
symbol , -
loop-.
with var1 [type1] = expression1 {and var [type] = expression}*
var -
expression, , -
let.
initially expression1 expression*
loop-.
finally expression1 expression*
loop-.
for var [type1] for-rest1 {and var [type] for-rest}*
var ,
for-rest . -
for-, and,
,
do. for-rest :
[upfrom start] [{upto | below} end] [by step]

. ,
. -
start 0. -
step 1. upto
below : upto -
, end,
below . from to -
from to, from
upfrom.
333

downfrom start [{downto | above} end] [by step]


,
. start.
step 1.
downto above
: downto , -
end, above .
{in | on} list [by function]
in, -
list, -
on, -
. function, -
cdr . -
:
.
= expression1 [then expression2]
expression1. -
then, -
expression2, expression1.
across vector
-
. : -
.
being the hash-keys of hash-table [using (hash-value v2)]
being the hash-values of hash-table [using (hash-key v2)]

- ( ), v2
( ) .
, v2
. : -
-.
being each {symbol | present-symbol | external-symbol} [of package]
-
(symbol), (present-symbol)
(external-symbol) . packa-
ge , find-package: ,
. :
.
do expression1 expression*
.
return expression
loop-
expression loop-.
334 D

{collect | append | nconc} expression [into var]


( nil) .
collect, expression -
; append, expression -
append; nconc, -
.
var, ,
loop- .
loop collect, append nconc -
.
var, -
.
{count | sum | maximize | minimize} expression [into var] [type]
.
count, -
expressi-
on; sum, -
; maximize/minimize, / -
. count sum -
0, maximize minimize .
var, ,
loop- .
type .
loop sum count -
. -
var,
.
maximize minimize.
when test then-clause1 {and then-clause}*
[else else-clause1 {and else-cause}*] [end]
test. , -
then-clause, else-clause.
then-clause else-clause do, return,
when, unless, collect, append, nconc, count, sum, maximize minimize.
then-clause1 else-clause1 it,
test.
unless test then-clause1 {and then-clause}*
[else else-clause1 {and else-clause}*] [end]
unless test e1en when (not test)
e1en.
repeat integer
:
integer .
335

while expression
: ,
expression .
until expression
while (not expression).
always expression
while, -
loop: nil, expression , t.
never expression
always (not expression).
thereis expression
until, -
loop: t, expression , nil .
(loop-finish)
loop-, -
loop-,
loop- .

(add-method generic-function method)


method generic-function, -
generic-function. -
. method
.
(allocate-instance class &rest initargs &key)
class -
. .
(call-method method &optional next-methods)
method, -
, . next-method, ,
, me-
thod. method -
(make-method expression), expression . -
expression , -
, call-
method.
(call-next-method &rest args)
-
args, . -
args,
336 D

( ).
call-next-method -
around-. -
, no-next-method, -
.
(change-class instance class &rest initargs &key)
instance class, in-
stance. , -
class, ,
. , -
class, update-instance-for-
redefined-class. .
(class-name class)
class. setf.
(class-of object)
, object.
(compute-applicable-methods
generic-function args)

generic-function, -
args.
(defclass name (superclass*) (slot-spec*) class-spec*)
name. name
,
. , ,
superclass, .
-
, slot-spec. -
. 428.
slot-spec symbol
(symbol k1 v1*kn vn*), k -
. symbol . -
k :
:reader fname*
fname
fname,
.
:writer fname*
fname
fname, -
setf (
).
337

:accessor fname*
fname
fname, .
setf.
:allocation where
where :instance ( ), -
; :class, -
.
:initform expression

, -
expression.
:initarg symbol*
symbol

make-instance.
. -
initarg .
:type type
type , .
:documentation string
.
class-spec : (:docu-
mentation string), (:metaclass symbol) (:default-initargs k1 e1kn en).
; . make-instan-
ce. make-instance
k -
e, . :documentation -
, :metaclass -
, standard-class.
(defgeneric fname parameters entry*)
-
fname. . ,
fname .
parameters -; . defmethod.
-
, parameters.
entry :
(:argument-precedence-order parameter*)
,
defgeneric.
.
338 D

(declare (optimize property*))


-
, , .
. . declare.
(:documentation string)
fname function.
(:method-combination symbol arguments*)
,
symbol. - -
,
define-method-combination.
(:generic-function-class symbol)
sym-
bol.
. standard-gene-
ric-function.
(:method-class symbol)
, -
symbol. -
. standard-method.
(:method qualifier* parameters . body)
(defmethod fname qualifier* parameters . body). entry
.
(define-method-combination symbol property*)
: .

. c1cn (
) -
symbol,
(symbol c1cn). property :
:documentation string
symbol method-combi-
nation.
:identity-with-one-argument bool
,
.
bool , , ,
. , ,
and- progn- .
339

:operator opname
( symbol),
; opname -
-.
(define-method-combination symbol parameters
(group-spec*)
[(:arguments . parameters2)]
[(:generic-function var)]
. body)
:
. -
, symbol-, -
, body.
-
call-method.
, body, , -
. parameters
, symbol :method-combi-
nation defgeneric. parameters2 ( ) , -
. -
initform--
. , &whole, -
. var ( )
.
group-spec -
. -
group-spec (var {pattern* | predname}
option*). var ,
pattern -
predname. ( ,
.) -
*, -
, , (equal) -
. ( *
.)
var,
-
. :
:description format
format -
format, -
.
340 D

:order order
order :most-specific-first ( ),
; -
:most-specific-last, .
:required bool
bool , -
.
(defmethod fname qualifier* parameters . body)
fname; -
, . . ,
fname .
qualifier , -
. -
:before, :after :around.
parameter ,
, -
(name specialization), specialization
( ) (eql expres-
sion) ( ).
. -
, defmethod
(eql) expression. -
, -
, . parameters
-
, .
(lambda parms . body) -
, parms parameters -
. defun, -
fname, fname , f, fname
(setf f).
(ensure-generic-function fname &key
argument-precedence-order
declare documentation
environment generic-function-class
lambda-list method-class
method-combination)
fname (
) . -
, -
. ar-
gument-precedence-order, declare, documentation method-combinati-
on ; lambda-list
341

; generic-function-class
, -
change-class. method-
class, .
(find-class symbol &optional error environment)
, environment symbol.
, , error -
( ), nil.
setf; , -
find-class nil.
(find-method generic-function
qualifiers specializers
&optional error)
generic-function, -
qualifiers, speciali-
zers. specializers ( ); t
. , error
, nil.
(function-keywords method)
: , -
method; , method
.
(initialize-instance instance &rest initargs &key)
shared-initialize -
instance initargs. ma-
ke-instance. .
(make-instance class &rest initargs &key)
class. initargs
-: k1 v1kn vn. -
: -
k , -
v; ,
initarg- ,
,
; , initform,
; . -
.
(make-instance-obsolete class)
defclass
class. ( update-instance-
for-redefined-class) class.
342 D

(make-load-form object &optional environment)


object , , -
, ,
environment ,
object .
(make-load-form-saving-slots instance
&key slot-names environment)
, environment
, instance .
slot-names,
.
(method-qualifiers method)
.
(next-method-p)
, -
.
(no-applicable-method generic-function
&rest args)
, generic-function -
args . -
.
(no-next-method generic-function method
&rest args)
, method generic-func-
tion , .
args . -
.
(reinitialize-instance instance &rest initargs)
instance
initargs. sha-
red-initialize ( nil).
.
(remove-method generic-function method)
method generic-
function, generic-function.
(shared-initialize instance names
&rest initargs &key)
instance initargs.
initform,
names names t. -
.
343

(slot-boundp instance symbol)


, symbol -
instance . ,
slot-missing.
(slot-exists-p object symbol)
, object symbol.
(slot-makunbound instance symbol)
symbol instance.
(slot-missing class object symbol opname
&optional value)
, opname
symbol object class. (
value,
.) .
(slot-unbound class instance symbol)
, slot-value symbol
instance ( class), -
. .
, -
slot-value.
(slot-value instance symbol)
symbol instance.
, slot-missing, slot-
unbound. .
setf.
(with-accessors ((var fname)*) instance
declaration* expression*)
, var
instance. -
fname .
(with-slots ({symbol | (var symbol)}*) instance
declaration* expression*)
, (
var, ) -,
symbol instance.
(unbound-slot-instance condition)
, -
condition.
344 D

(update-instance-for-different-class old new


&rest initargs
&key)
change-class -
. old -
; new -
, . -
shared-initialize
: new, initargs.
.
(update-instance-for-redefined-class
instance added deleted plist
&rest initargs)
make-instance-obsolete
; added -
, deleted ( , -
); plist
(name . val) deleted, val.
shared-initialize -
: instance, added initargs.

(copy-structure structure)
, structure, -
(eql) .
(defstruct {symbol | (symbol property*)} [string] field*)
symbol, sym-
bol. symbol ,
, -
, , . symbol str, -
: make-str,
; str-p, str;
copy-str, str; -
str; str.
string, , sym-
bol structure. -
symbol type, -
.
property :
:conc-name | (:conc-name [name])
f str
namef str-f ( ). name -
nil, f.
345

:constructor | (:constructor [name [parameters]])


name nil, -
name. name nil,
. , -
, make-str.
parameters, --
,
, .
.
:copier | (:copier [name])
name nil, -
name. name nil, -
. , -
, copy-str.
(:include name field*)
, str
name.
. fields :include
(. ); -
( -
), , -
( -
). :type (. ), -
,
.
(:initial-offset i)
i
. :type.
:named
, -
. :type.
:predicate | (:predicate [name])
name nil,
name. name nil, -
. ,
, str-p. -
:type , :named.
(:print-function [fname])
str , fname (
-) -
: , , -
, , . -
print-object.
:type.
346 D

(:print-object [fname])
:print-function, -
. - : :print-func-
tion, :print-object.
(:type {vector | (vector type) | list})
-
.
; -
, ( -
:named) . :type
(vector type), :named , type
symbol.
field name (name
[initform property*]).
, -
:include. -
;
str str-name,
:conc-name. name
str, -
.
initform, ,
, -
defstruct. initform , -
. property :
:type type
,
type.
:read-only1 bool
bool nil, .


(abort &optional condition)
, (find-restart abort condition).
(assert test [(place*) [cond arg*]])
test nil,
, cond args. test -
places; -

1
, . .
.
347

. -
nil.
(break &rest args)
format args, .
.
(cell-error-name condition)
, , -
error condition.
(cerror format cond &rest args)
error, -
, nil. for-
mat format .
(check-type place type [string])
, place
type. string, -
.
(compute-restarts &optional condition)
, .
condition, ,
-
, . -
.
(continue &optional condition)
(find-restart abort condition) ,
, nil.
(define-condition name (parent*) (slot-spec*) class-spec*)
.
, defclass, ,
class-specs :metaclass,
:report. :report -
. -
, (
), .
(error cond &rest args)
cond args. -
, .
(find-restart r &optional condition)
r,
r , , (eq) r, r . -
condition, ,
348 D

, . -
, nil.
(handler-bind ((type handler)*) expression*)
expression -
. ,
( ), handler,
type .
( ), .
, -
, -
handler-bind.
(handler-case test
(type ([var]) declaration* expression*)*
[(:no-error parameters declaration* expression*)])
test.
type, , hand-
ler-case () expressi-
ons, , var ( -
) .
:no-error, handler-case -
test. :no-error, handler-case
() , parameters -
, test. :no-error
, .
(ignore-errors expression*)
progn, , exp-
ression .
, : nil
.
(invalid-method-error method format &rest args)
,
. format
arg format .
(invoke-debugger condition)
condition.
(invoke-restart restart &rest args)
restart , args;
,
args.
(invoke-restart-interactively restart)
invoke-restart,
.
349

(make-condition type &rest initargs)


type. , -
make-instance.
(method-combination-error format &rest args)
-
. format
.
(muffle-warning &optional condition)
, (find-restart muffle-warning condi-
tion).
(restart-bind ((symbol function {key val}*)*) expression*)
.
symbol -
function. ( symbol nil, -
.) key :
:interactive-function
val
, invoke-restart.
.
:report-function
val
(), -
.
:test-function
val
(), , -
. -
.
(restart-case test (symbol parameters {key val}*
declaration* expression*)*)
test . -
symbol (lambda parame-
ters declaration* expression*). ( symbol nil, -
.) key :
:interactive
val -
-, , -
invoke-restart. -
.
350 D

:report
val , -
, --
, (), -
.
:test
val -
-, (-
), , -
. .
(restart-name restart)
restart nil, .
(signal cond &rest args)
, cond args. -
, nil.
(simple-condition-format-arguments condition)
format simple-condition.
(simple-condition-format-control condition)
( ) format simple-condition.
(store-value object &optional condition)
, (find-restart store-value condi-
tion) object. , nil.
(use-value object &optional condition)
, (find-restart use-value condition)
object. , nil.
(warn cond &rest args)
simple-warning, cond
args. ,
*error-output* nil.
(with-condition-restarts condition restarts expression*)
condition,
restarts. -
expression.
(with-simple-restart (symbol format arg*) expression*)
expression sym-
bol, , , with-simple-
restart : nil t. format args
format .
351

(boundp symbol)
, symbol -
.
(copy-symbol symbol &optional props-too)
,
(string=) symbol. props-too ,
symbol-value symbol-function, symbol,
symbol-plist.
(gensym &optional prefix)
.
"G" -
*gensym-counter*. -
prefix, "G".
(gentemp &optional (prefix "T") package) []
, package.
prefix ,
, .
(get symbol key &optional default)
symbol (k1 v1kn vn) key (eq)
k, v k. -
default, ( k).
setf. k
(keywordp object)
, object key-
word.
(make-symbol string)
,
(string=) string.
(makunbound symbol)
symbol, -
. (boundp symbol) -
. symbol.
(set symbol object) []
(setf (symbol-value symbol) object).
(symbol-function symbol)
symbol.
, .
setf.
352 D

(symbol-name symbol)
, symbol.
.
(symbolp object)
, object .
(symbol-package symbol)
symbol.
(symbol-plist symbol)
symbol. -
setf.
(symbol-value symbol)
symbol. -
, .
setf.
(remprop symbol key)

symbol key . , -
key .

(defpackage name property*)


name ( , )
. -
, ; .
property :
(:nicknames name*)
name ( ) -
.
(:documentation string)
string .
(:use package*)
, package; . use-pack-
age.
(:shadow name*)
name ; -
;
. shadow.
353

(:shadowing-import-from package name*)


name ; -
package ,
shadowing-import.
(:import-from package name*)
name ; -
package ,
import.
(:export name*)
name ; -
package; . export.
(:intern name*)
name ; -
, ;
. intern.
(:size integer)
.
property, :documentation :size, -
. :
:shadow :shadowing-import-from, :use, :import-from :intern,
:export. ,
toplevel.
(delete-package package)
package , -
. ,
package .
(do-all-symbols (var [result])
declaration* {tag | expression}*)
do-symbols, -
.
(do-external-symbols (var [package [result]])
declaration* tag | expression}*)
do-symbols, -
package.
(do-symbols (var [package [result]])
declaration* {tag | expression}*)
, var ,
package. , -
, . tagbody,
do-symbols nil. ()
354 D

result nil, result . -


result var, nil.
(export symbols &optional package)
, ( symbols
, ), package. -
t.
(find-all-symbols name)
, -
name ( name ) name ( name ).
(find-package package)
, package, nil, -
.
(find-symbol string &optional package)
string, package. -
, : :internal,
:external :inherited. string
, nil.
(import symbols &optional package)
symbols ( -
; symbols , ),
, package. ,
, package. t.
(in-package name)
, name (
). toplevel, -
.
(intern string &optional package)
, package, , (string=)
string, . -
, : :internal, :external,
:inherited nil ( , ).
(list-all-packages)
.
(make-package name &key nicknames use)
name ( , name
), nicknames (
nicknames ) -
, use ( /
).
355

(package-error-package condition)
, condition, -
.
(package-name package)
, package, nil,
.
(package-nicknames package)
,
package.
(packagep object)
, object .
(package-shadowing-symbols package)
package.
(package-used-by-list package)
, package.
(package-use-list package)
, package.
(rename-package package name &optional nicknames)
package name ( ) -
name ( ), -
nicknames -
. .
(shadow names &optional package)
names , , / -
. -
package.
package. t.
(shadowing-import symbols &optional package)
symbols ( -
; symbols , )
package .
, . -
t.
(unexport symbols &optional package)
( symbols , -
) package. t.
356 D

(unintern symbol &optional package)


symbol package (
). package
symbol, . ,
symbol package.
(unuse-package packages &optional package)
use-package, . -
t.
(use-package packages &optional package)
packages ( -
, , ) package.
keyword. t.
(with-package-iterator (symbol packages key*)
declaration* expression*)
expression, symbol -
, packa-
ges ( , , ).
:internal, :external :inherited .
:
, ( nil
); ; , -
(:internal, :external :inherited); ,
.
,
, .

(abs n)
-
, n.
(acos n)
n ( ).
(acosh n)
n.
(arithmetic-error-operands condition)
condition.
(arithmetic-error-operation condition)
( ) condi-
tion.
357

(ash i pos)
, -
1 i pos ( , pos ).
(asin n)
n ( ).
(asinh n)
n.
(atan n1 &optional (n2 1))
n1/n2 ( ).
(atanh n)
n.
(boole op i1 i2)
, -
op i1 i2. Common Lisp -
16 , -
. , boole
:

boole-1 i1
boole-2 i2
boole-andc1 (logandc1 i1 i2)
boole-andc2 (logandc2 i1 i2)
boole-and (logand i1 i2)
boole-c1 (lognot i1)
boole-c2 (lognot i2)
boole-clr 0
boole-eqv (logeqv i1 i2)
boole-ior (logior i1 i2)
boole-nand (lognand i1 i2)
boole-nor (lognor i1 i2)
boole-orc1 (logorc1 i1 i2)
boole-orc2 (logorc2 i1 i2)
boole-set 1
boole-xor (logxor i1 i2)

1
(twos complement)
. -
: 0 , 1 .
-
. . .
358 D

(byte length pos)


length , -
2pos.
(byte-position spec)
log2 ,
- spec.
(byte-size spec)
- spec.
(ceiling r &optional (d 1))
: i,
r/d, r- i d. d -
.
(cis r)
(cos r)
(sin r).
(complex r1 &optional r2)
r1 -
r2 , r2 .
(complexp object)
, object -
.
(conjugate n)
n: n, n -
, #c(a b), n #c(a b).
(cos n)
n ( ).
(cosh n)
n.
(decf place [n])
place n 1, n .
(decode-float f)
: f, (-1.0
, 1.0).
, f,
.
(denominator rational)
rational a/b, b.
359

(deposit-field new spec i)


i --
spec new.
(dpb new spec i)
s new i -
- spec, s.
(evenp i)
, i .
(exp n)
en.
(expt n1 n2)
n1n2.
(fceiling r &optional (d 1))
ceiling,
.
(ffloor r &optional (d 1))
floor,
.
(float n &optional f)
n f
single-float, f .
(float-digits f)
f.
(floatp object)
, object -
.
(float-precision f)
-
f.
(float-radix f)
f.
(float-sign f1 &optional (f2 (float 1 f1)))
f2 -
f1.
360 D

(floor r &optional (d 1))


: i,
r/d, r- i d. d -
.
(fround r &optional (d 1))
round,
.
(ftruncate r &optional (d 1))
truncate, -
.
(gcd &rest is)
0,
.
(imagpart n)
n.
(incf place [n])
place n 1, n .
(integer-decode-float f)
, ,
, decode-float.
(integer-length i)
, i -
.
(integerp object)
, object .
(isqrt i)
, -
i (i ).
(lcm &rest is)
1, -
.
(ldb spec i)
, -
i - spec. -
setf.
(ldb-test spec i)
, i --
spec 1.
361

(log n1 &optional n2)


logn2 n1 loge n1, n2 .
(logand &rest is)
-
0, .
(logandc1 i1 i2)
-
i2 i1.
(logandc2 i1 i2)
-
i1 i2.
(logbitp pos i)
, pos -
i 1. 0.
(logcount i)
i,
i , .
(logeqv &rest is)
-
1 -1, -
.
(logior &rest is)
-
0,
.
(lognand i1 i2)
-
.
(lognor i1 i2)
-
.
(lognot i)
, -
i.

1
XNOR, , ,
. . .
362 D

(logorc1 i1 i2)

i2 i1.
(logorc2 i1 i2)

i1 i2.
(logtest i1 i2)
,
i1 i2.
(logxor &rest is)
-
-
, 0, .
(make-random-state &optional state)
. state -
, ; nil, -
*random-state*; t, , -
.
(mask-field spec i)
, -
, i , - spec.
(max r1 &rest rs)
.
(min r1 &rest rs)
.
(minusp r)
, r .
(mod r1 r2)
, floor -
.
(numberp object)
, object .
(numerator rational)
rational a/b, -
a.
(oddp i)
, i .
363

(parse-integer string &key start end radix junk-allowed)


: , (
10), -
. start end
. string , + -
, -
. ( -
.) junk-allowed ( ), -
string, ,
; , parse-integer nil, -
.
(phase n)
n, -
.
(plusp r)
, r .
(random limit &optional (state *random-state*))
, limit (
) -
. state
( ).
(random-state-p object)
, object -
.
(rational r)
r . r
, .
(rationalize r)
r . r
, -
.
(rationalp object)
, object -
.
(realp object)
, object
.
(realpart n)
n.
364 D

(rem r1 r2)
truncate .
(round r &optional (d 1))
: , r/d, r- i d.
r/d , .
d .
(scale-float f i)
f ri, r -
.
(signum n)
n , 1
, 0 , -1 .
n , -
, 1, .
(sin n)
n ( ).
(sinh n)
n.
(sqrt n)
n.
(tan n)
n ( ).
(tanh n)
n.
(truncate r &optional (d 1))
: i, -
r/d, r- i d.
d .
(upgraded-complex-part-type type)
-
, type.
(zerop n)
, n .
(= n1 &rest ns)
, .
(/= n1 &rest ns)
, .
365

(> r1 &rest rs)


,
.
(< r1 &rest rs)
,
.
(>= r1 &rest rs)
,
.
(<= r1 &rest rs)
,
.
(* &rest ns)
1,
.
(+ &rest ns)
0, .
(- n1 &rest ns)
, -n1. (- a1
an) a1 - - an.
(/ n1 &rest ns)
( ),
. ,
, -
( .)
(1+ n)
(+ n 1).
(1- n)
(- n 1).

(alpha-char-p char)
, char .
(both-case-p char)
, char .
(alphanumericp char)
, char .
366 D

(character c)
, ,
, .
(characterp object)
, object .
(char-code char)
code char. -
, ASCII-.
(char-downcase char)
char , -
; char.
(char-greaterp char1 &rest chars)
char>, .
(char-equal char1 &rest hars)
char=, .
(char-int char)
, char.
, ,
, char-code.
(char-lessp char1 &rest chars)
char<, .
(char-name char)
char nil, .
(char-not-greaterp char1 &rest chars)
char<=, .
(char-not-equal char1 &rest chars)
char/=, .
(char-not-lessp char1 &rest chars)
char>=, .
(char-upcase char)
char , -
, char.
(char= char1 &rest chars)
, .
(char/= char1 &rest chars)
, .
367

(char< char1 &rest chars)


,
.
(char> char1 &rest chars)
,
.
(char<= char1 &rest chars)
,
.
(char>= char1 &rest chars)
,
.
(code-char code)
char, code.
(digit-char i &optional (r 10))
, i -
r.
(digit-char-p char &optional (r 10))
, char
r.
(graphic-char-p char)
, char .
(lower-case-p char)
, char .
(name-char name)
name ( name, ).
.
(standard-char-p char)
, char .
(upper-case-p char)
, char .

(acons key value alist)


(cons (cons key value) alist).
368 D

(adjoin object prolist &key key test test-not)


member , pro-
list; (cons object prolist).
(append &rest prolists)
prolists.
, , ,
(cdr (append (a) x) (eq) x. nil,
.
(assoc key alist &key test test-not)
alist, car key.
(assoc-if predicate alist &key key)
alist, pre-
dicate.
(assoc-if-not predicate alist &key key) []
alist, predi-
cate.
(atom object)
, object cons-.
(butlast list &optional (n 1))
(nbutlast list &optional (n 1))
list n nil,
list n . , n
.
(car list)
list , car. list nil, nil.
setf.
(cdr list)
list , cdr. list nil, nil.
setf.
(cxr list)
x ,
a d. car cdr. -
, (cdaar x) (cdr (car (car x))).
setf.
(cons object1 object2)
, car object1, cdr object2. -
object2 (e1en),
(object1 e1en).
369

(consp object)
, object cons-.
(copy-alist alist)
(mapcar #(lambda (x) (cons (car x) (cdr x))) alist).
(copy-list list)
, (equal) list,
. list nil,
nil.
(copy-tree tree)
, tree,
. tree , tree.
(endp list)
, list nil.
(first list) (tenth list)
list nil,
. -
setf.
(getf plist key &optional (default nil))
plist (p1 v1pn vn) pi , (eq) key,
vi. , default. -
setf.
(get-properties plist prolist)
plist (p1 v1pn vn) pi , (eq) -
- prolist, pi, vi (pi vipn vn). -
nil.
(intersection prolist1 prolist2 &key test test-not)
(nintersection prolist1 prolist2 &key test test-not)
prolist1, prolist2. -
.
(last list &optional (n 1))
n list list,
n . n 0, cdr -
list.
(ldiff list object)
object list, -
object. list.
(list &rest objects)
, objects.
370 D

(list* object &rest objects)


, . -
(list* arg1argn) (nconc (list arg1argn-1)
argn).
(list-length list)
list nil, -
( length,
). list , -
.
(listp object)
, object , cons--
nil.
(make-list n &key (initial-element nil))
n initial-element.
(mapc function prolist &rest prolists)
function n (n prolist):
prolist, -
prolist, n-. -
prolist.
(mapcan function prolist &rest prolists)
nconc mapcar
.
(mapcar function prolist &rest prolists)
function n (n prolist):
prolist, -
prolist, n-. -
, function.
(mapcon function prolist &rest prolists)
nconc maplist
.
(mapl function prolist &rest prolists)
function n (n prolist):
prolist , -
cdr prolist, (n1)-. prolist.
(maplist function prolist &rest prolists)
function n (n prolist):
prolist , -
cdr prolist, (n1)-. -
, function.
371

(member object prolist &key key test test-not)


prolist, , -
object, nil, .
(member-if predicate prolist &key key test test-not)
prolist, , -
predicate , nil, .
(member-if-not predicate prolist &key key test test-not) []
prolist, , -
predicate , nil, .
(nconc &rest lists)
lists ( ).
: cdr -
.
. , nil.
(nth n list)
(n+1)- list. nil,
list (n+1) .
setf.
(nthcdr n list)
n- cdr list.
(null object)
, object nil.
(pairlis keys values &optional alist)
, (nconc (mapcar #cons keys values)
alist) (nconc (nreverse (mapcar #cons keys values)) alist). ,
keys values .
(pop place)
place,
list, (cdr list). (car list).
(push object place)
place (cons object place).
.
(pushnew object place &key key test test-not)
place,
, adjoin -
. place.
(rassoc key alist &key key test test-not)
alist, cdr key.
372 D

(rassoc-if predicate alist &key key)


alist, cdr predicate
.
(rassoc-if-not predicate alist &key key)
alist, cdr predicate
.
(remf place key)
, place, plist.
plist (p1 v1pn vn) pi p, (eq) key, -
pi vi plist place.
, - , -
.
(rest list)
cdr. setf.
(revappend list1 list2)
(nreconc list1 list2)
(nconc (reverse list1) list2) (nconc (nreverse list1)
list2) .
(rplaca cons object)
(setf (car cons) object), cons.
(rplacd cons object)
(setf (cdr cons) object), cons.
(set-difference prolist1 prolist2 &key key test test-not)
(nset-difference prolist1 prolist2 &key key test test-not)
prolist1, prolist2. -
.
(set-exclusive-or prolist1 prolist2 &key key test test-not)
(nset-exclusive-or prolist1 prolist2 &key key test test-not)
, prolist1,
prolist2, .
.
(sublis alist tree &key key test test-not)
(nsublis alist tree &key key test test-not)
, tree, , -
alist, .
, tree.
(subsetp prolist1 prolist2 &key key test test-not)
, prolist1 prolist2.
373

(subst new old tree &key key test test-not)


(nsubst new old tree &key key test test-not)
, tree, , -
old, new.
(subst-if new predicate tree &key key)
(nsubst-if new predicate tree &key key)
, tree, , -
predicate, new.
(subst-if-not new predicate tree &key key) []
(nsubst-if-not new predicate tree &key key) []
, tree, , -
predicate, new.
(tailp object list)
, object list,
object nil, , list.
(tree-equal tree1 tree2 &key test test-not)
, tree1 tree2
.
(union prolist1 prolist2 &key key test test-not)
(nunion prolist1 prolist2 &key key test test-not)
, prolist1 prolist2. -

. prolist1, prolist2 -
,
.

(adjustable-array-p array)
, array .
(adjust-array array dimensions &key ...)
, array ( array, -
) . -
dimensions , ;
, -
:initial-element. , make-array, -
:
:element-type type
type .
374 D

:initial-element object
object,
.
:initial-contents seq
make-array, array -
.
:fill-pointer object
object nil, ( ) -
.
:displaced-to array2
array , ar-
ray2 nil,
, -
( ) :initial-
element ( ). -
array2 , -
array2.
, make-array.
:displaced-index-offset i
,
.
(aref array &rest is)
array is (
, is
). . -
setf.
(array-dimension array i)
i- .
.
(array-dimensions array)
,
.
(array-displacement array)
: ,
array, . nil 0, array -
.
(array-element-type array)
array.
(array-has-fill-pointer-p array)
, array .
375

(array-in-bounds-p array &rest is)


, -
aref.
(arrayp object)
, object .
(array-rank array)
array.
(array-row-major-index array &rest is)
is, -
array . -
.
(array-total-size array)
array.
(bit bit-array &rest is)
aref - bit-array. -
setf.
(bit-and bit-array1 bit-array2 &optional arg)
- logand : -
- -
, . arg t, -
; arg nil, bit-array1;
arg - ( ), .
(bit-andc1 bit-array1 bit-array2 &optional arg)
bit-and, logandc1.
(bit-andc2 bit-array1 bit-array2 &optional arg)
bit-and, logandc2.
(bit-eqv bit-array1 bit-array2 &optional arg)
bit-and, logaeqv.
(bit-ior bit-array1 bit-array2 &optional arg)
bit-and, logior.
(bit-nand bit-array1 bit-array2 &optional arg)
bit-and, lognand.
(bit-nor bit-array1 bit-array2 &optional arg)
bit-and, lognor.
(bit-not bit-array &optional arg)
- lognot : -
bit-array. arg t, -
376 D.

; arg nil, bit-


array1; arg - ( ),
.
(bit-orc1 bit-array1 bit-array2 &optional arg)
bit-and, logorc1.
(bit-orc2 bit-array1 bit-array2 &optional arg)
bit-and, logorc2.
(bit-xor bit-array1 bit-array2 &optional arg)
bit-and, logxor.
(bit-vector-p object)
, object -.
(fill-pointer vector)
vector. -
setf, , -
.
(make-array dimensions &key element-type initial-element
initial-contents adjustable
fill-pointer displaced-to
displaced-index-offset)
dimensions ( -
, dimensions ). -
,
. :
:element-type type
type .
:initial-element object
object.
:initial-contents.
:initial-contents seq

seq. -
.
:initial-element.
:adjustable object
object ,
; ,
.
:fill-pointer object
object , ( )
. object
377

, -
.
:displaced-to array
array. -
-
array ( , -
, ).
:displaced-index-offset i
i.
:displaced-to.
(row-major-aref array i)
i- array, -
.
. setf.
(sbit simple-bit-array &rest is)
aref -. -
setf.
(simple-bit-vector-p object)
, object -.
(simple-vector-p object)
, object .
(svref simple-vector i)
i- simple-vector.
. setf.
(upgraded-array-element-type type &optional env)
,
, :element-type type.
(vector &rest objects)
objects.
(vectorp &rest objects)
, object .
(vector-pop vector)
vector ,
. -
, 0, .
(vector-push object vector)
vector,
nil. ,
378 D.

, object, -
. -
.
(vector-push-extend object vector &optional i)
vector-push, -
vector, i (
, )
adjust-array.

(char string i)
i- string. .
. -
setf.
(make-string n &key initial-element
(element-type character))
n initial-element (
).
(schar simple-string i)
char, .
setf.
(simple-string-p object)
, object .
(string arg)
arg , ; , -
; , .
(string-capitalize string &key start end)
(nstring-capitalize string &key start end)
, -
, . -
. -
string-capitalize ,
.
(string-downcase string &key start end)
(nstring-downcase string &key start end)
string-upcase nstring-upcase, -
string .
(string-equal string1 string2 &key
start1 end1 start2 end2)
string=, .
379

(string-greaterp string1 string2 &key


start1 end1 start2 end2)
string>, .
(string-upcase string &key start end)
(nstring-upcase string &key start end)
, -
. -
start end , -
. string-upcase ,
.
(string-left-trim seq string)
string-trim, c .
(string-lessp string1 string2 &key
start1 end1 start2 end2)
string<, .
(string-not-equal string1 string2 &key
start1 end1 start2 end2)
string/=, .
(string-not-greaterp string1 string2 &key
start1 end1 start2 end2)
string<=, .
(string-not-lessp string1 string2 &key
start1 end1 start2 end2)
string>=, .
(stringp object)
, object .
(string-right-trim seq string)
string-trim, .
(string-trim seq string)
, string, , -
seq, .
(string= string1 string2 &key start1 end1 start2 end2)
, string1 string2
. start1
end1, start2 end2 start end string1
string2 .
(string/= string1 string2 &key start1 end1 start2 end2)
, string= .
380 D.

(string< string1 string2 &key start1 end1 start2 end2)


, -
-
;
char<. -
, string=.
(string> string1 string2 &key start1 end1 start2 end2)
, -
-
;
char>. -
, string=.
(string<= string1 string2 &key start1 end1 start2 end2)
, string< string= -
.
(string>= string1 string2 &key start1 end1 start2 end2)
, string> string= -
.

(concatenate type &rest sequences)


type
sequences. -
, .
(copy-seq proseq)
-
, proseq.
(count object proseq &key key test test-not
from-end start end)
object proseq.
(count-if predicate proseq &key key from-end start end)
proseq, -
predicate.
(count-if-not predicate proseq &key key from-end start end) []
predicate
proseq.
(elt proseq n)
(n+1)- proseq. proseq n+1,
. setf.
381

(fill proseq object &key start end)


proseq object. pro-
seq.
(find object proseq &key key test test-not from-end start end)
object proseq.
(find-if predicate proseq &key key from-end start end)
proseq.
(find-if-not predicate proseq &key key from-end start end) []
proseq.
(length proseq)
proseq. -
proseq , -
, .
(make-sequence type n &key (initial-element nil))
type n ini-
tial-element.
(map type function proseq &rest proseqs)
function n (n proseq):
proseq, -
n- proseq. -
type, function. ( type nil,
mapc .)
(map-into result function proseq &rest proseqs)
function n (n proseq,
result): proseq,
n- proseq, -
n result , -
function, result.
(merge type sequence1 sequence2 predicate &key key)
(stable-sort (concatenate type sequence1 sequence2) pre-
dicate :key key), .
(mismatch sequence1 sequence2
&key key test test-not from-end
start1 end1 start2 end2)
( ) -
sequence1, sequence1
sequence2. , -
nil. start1 end1, start2 end2
start end sequence1 sequence2 .
382 D.

(position object proseq


&key key test test-not from-end start end)
( ) -
proseq, object.
(position-if predicate proseq &key key from-end start end)
( ) -
proseq, predicate.
(position-if-not predicate proseq []
&key key from-end start end)
( ) -
proseq, predicate.
(reduce function proseq
&key key from-end start end initial-value)
reduce , f -
, proseq a, b c:
from-end initial-value
(f (f a b) c)
(f (f (f initial-value a) b) c)
(f a (f b c))
(f a (f b (f c initial-value)))

proseq initial-value
, . proseq -
initial-value, , initial-value
, . -
key initial-value, key initial-value.
(remove object proseq
&key key test test-not from-end start end count)
(delete object proseq
&key key test test-not from-end start end count)
, proseq, -
, object. count,
count .
(remove-duplicates proseq
&key key test test-not from-end start end)
(delete-duplicates proseq
&key key test test-not from-end start end)
, proseq,
, .
383

(remove-if predicate proseq


&key key from-end start end count)
(delete-if predicate proseq
&key key from-end start end count)
, proseq, -
, predicate. -
count, count .
(remove-if-not predicate proseq []
&key key from-end start end count)
(delete-if-not predicate proseq []
&key key from-end start end count)
, proseq, -
, predicate. -
count, count .
(replace sequence1 sequence2
&key start1 end1 start2 end2)
sequence1 sequence2 sequen-
ce1. -
. ,
(eq), , -
( ).
(reverse proseq)
(nreverse proseq)
, proseq, -
. ,
reverse, . proseq ,
reverse .
(search sequence1 sequence2
&key key test test-not from-end start1 end1 start2 end2)
( ) -
sequence1 sequence2.
, -
nil.
(sort proseq predicate &key key)
, proseq,
, , -
e f (predicate e f) , (predicate
f e) .
(stable-sort proseq predicate &key key)
sort, -
proseq.
384 D.

(subseq proseq start &optional end)


, -
proseq. start end
: start (
) , end,
, -
. setf,
replace.
(substitute new old proseq
&key key test test-not from-end start end count)
(nsubstitute new old proseq
&key key test test-not from-end start end count)
, proseq,
, old, new. -
count, count .
(substitute-if new predicate proseq
&key key from-end start end count)
(nsubstitute-if new predicate proseq
&key key from-end start end count)
, proseq,
, predicate, new.
count, count -
.
(substitute-if-not new predicate proseq []
&key key from-end start end count)
(nsubstitute-if-not new predicate proseq []
&key key from-end start end count)
, proseq,
, predicate, new.
count, count -
.

-
(clrhash hash-table)
, hash-table, -
.
(gethash key hash-table &optional default)
, key hash-
table, default, . -
, . -
setf.
- 385

(hash-table-count hash-table)
hash-table.
(hash-table-p object)
, object -.
(hash-table-rehash-size hash-table)
, , :re-
hash-size make-hash-table. , -
hash-table .
(hash-table-rehash-threshold hash-table)
, , :re-
hash-threshold make-hash-table. ,
hash-table .
(hash-table-size hash-table)
hash-table.
(hash-table-test hash-table)
,
hash-table.
(make-hash-table &key test size rehash-size
rehash-threshold)
-, test (
eql) . size -
. rehash-size -
, ( integer,
, float, ).
rehash-threshold , -
.
(maphash function hash-table)
function, -
, hash-
table.
(remhash key hash-table)
hash-table , key.
, .
(sxhash object)
, , equal--.
fixnum -
(equal) .
386 D.

(with-hash-table-iterator (symbol hash-table)


declaration* expression*)
expressions, symbol
, -
hash-table. : -
, , ( nil -
, ), -
.


(directory-namestring path)
,
directory path.
(enough-namestring path &optional path2)
, -
path path2 ( path2
*default-pathname-defaults*).
(file-namestring path)
,
name, type version path.
(host-namestring path)
,
host path.
(load-logical-pathname-translations string)
string,
. , - -
.
(logical-pathname path)
, path.
(logical-pathname-translations host)
host,
.
(make-pathname &key host device directory
name type version defaults case)
, . -
defaults; -
, *default-pathname-de-
faults*, nil.
387

host . device
. directory ,
:wild. name type :wild.
version , :wild
:newest ( :oldest, :pre-
vious :installed). -
nil ( -
) :unspecific, -
( -
).
defaults (valid) -
pathname . , -
nil, -
defaults, .
case :local ( ),
; case :common, -
, ,
, , -
.
(merge-pathnames path &optional default-path version)
, -
path default-path ( *default-path-
name-defaults*). path name,
version ( :newest);
default-path.
(namestring path)
,
path.
(parse-namestring path &optional host default
&key start end junk-allowed)
path ,
. , -
, host ,
default ( ). -
(valid) junk-allowed, .
junk-allowed , nil. start end -
.
, .
(pathname path)
, path.
(pathname-host path &key case)
host path. :case -
, make-pathname.
388 D.

(pathname-device path &key case)


device path. :case -
, make-pathname.
(pathname-directory path &key case)
directory path. :case -
, make-pathname.
(pathname-match-p path wild-path)
, path wild-path; -
wild-path :wild.
(pathname-name path &key case)
name path. :case -
, make-pathname.
(pathnamep object)
, object .
(pathname-type path &key case)
type path. :case -
, make-pathname.
(pathname-version path &key case)
version path. :case -
, make-pathname.
(translate-logical-pathname path &key)
, path.
(translate-pathname path1 path2 path3 &key)
path1, wild- path2, -
, wild- path3.
(wild-pathname-p path &optional component)
, ,
component ( :host, :device, :directory, :name, :type
:version), wild ( component nil) path -
- wild-.

(delete-file path)
, path. t.
(directory path &key)
,
, path (
wild-).
389

(ensure-directories-exist path &key verbose)


, path, , -
( , verbose). -
: path , ,
- .
(file-author path)
, () path,
nil, .
(file-error-pathname condition)
, condition.
(file-write-date path)
( , get-universal-time), -
path, nil, -
.
(probe-file path)
, path,
nil, .
(rename-file path1 path2)
path1 path2 ( -
). file2
path1. : ,
.
(truename path)
, path. -
, .

(broadcast-stream-streams broadcast-stream)
, broadcast-stream.
(clear-input &optional stream)
stream -
nil.
(clear-output &optional stream)
stream -
nil.
(close stream &key abort)
stream, t, .
abort, -
390 D.

(, -
). , -
, open.
(concatenated-stream-streams concatenated-stream)
, concatena-
ted-stream .
(echo-stream-input-stream echo-stream)
echo-stream.
(echo-stream-output-stream echo-stream)
echo-stream.
(file-length stream)
stream nil, -
.
(file-position stream &optional pos)
pos , stream
nil, . pos ,
:start, :end ,
-
stream.
(file-string-length stream object)
stream
object.
, nil.
(finish-output &optional stream)
stre-
am, nil.
(force-output &optional stream)
finish-output, -
.
(fresh-line &optional stream)
stream,
.
(get-output-stream-string stream)
, ,
stream ( )
get-output-stream-string.
(input-stream-p stream)
, stream .
391

(interactive-stream-p stream)
, stream .
(listen &optional stream)
, ,
stream, .
(make-broadcast-stream &rest streams)
,
streams.
(make-concatenated-stream &rest input-streams)
, in-
put-streams.
(make-echo-stream input-stream output-stream)
-, input-stream
output-stream.
(make-string-input-stream string &optional start end)
, -
, string, .
start end , -
.
(make-string-output-stream &key element-type)
, element-
type. , -
get-output-stream-string.
(make-synonym-stream symbol)
, , -
symbol.
(make-two-way-stream input-stream output-stream)
,
input-stream output-stream.
(open path &key direction element-type if-exists
if-does-not-exist external-format)
, path, nil, -
.
:
:direction symbol
. -
: :input ( ) , :output -
, :io :probe, ,
.
392 D.

:element-type type
, . -
- character; -
integer,
signed-byte, unsigned-byte ( -
). -
character.
:if-exists symbol
, -
. : :new-version, -
, version path
:newest, :error;
:rename, ; :rename-and-
delete, ;
:overwrite,
; :append,
, ; :supersede, -
( ,
, ); nil, -
(open nil).
:if-does-not-exist symbol
, .
: :error, -
, direction :input, :if-exists
:overwrite :append; :create, ,
direction :output :io, if-exists
:overwrite, :append; nil, -
, direction :probe ( -
, open nil).
:external-format format
. -
format :default.
(open-stream-p stream)
, stream .
(output-stream-p stream)
, stream .
(peek-char &optional kind stream eof-error
eof-value recursive)
stream,
. kind nil, ;
t, -
; , -
393

, , .
, , eof-value
eof-error ( ; -
). recursive , peek-char
.
(read-byte stream &optional eof-error eof-value)
stream. ,
eof-value (
eof-error).
(read-char &optional stream eof-error eof-value recursive)
stream. -
, eof-
value eof-error ( ).
recursive , read-char
.
(read-char-no-hang &optional stream
eof-error eof-value recursive)
read-char, nil,
stream .
(read-line &optional stream eof-error eof-value recursive)
-
( , )
.
, eof-value -
eof-error ( ).
recursive , read-line -
.
(read-sequence proseq &optional stream &key start end)
stream proseq, -
. start end ,
.
(stream-element-type stream)
, -
stream.
(stream-error-stream condition)
, condition.
(stream-external-format stream)
stream.
(streamp object)
, object .
394 D.

(synonym-stream-symbol synonym-stream)
,
synonym-stream .
(terpri &optional stream)
stream.
(two-way-stream-input-stream two-way-stream)
two-way-stream.
(two-way-stream-output-stream two-way-stream)
two-way-stream.
(unread-char character &optional stream)
read-char stream. -
read-char
. peek-char.
(with-input-from-string (symbol string &key index
start end) declaration* expression*)
expression, symbol , -
make-string-input-stream, -
string, start end. ( -
.)
with-input-from-string
with-input-
from-string . index
( ),
setf; with-input-from-string , -

.
(with-open-file (symbol path arg*) declaration* expression*)
expression, symbol , -
path arg open. (-
-
.) with-open-file -
with-open-file
. ,
, .
(with-open-stream (symbol stream) declaration* expression*)
, symbol stream. (-
-
.) with-open-stream
with-
open-stream.
395

(with-output-to-string (symbol [string] &key element-type)


declaration* expression*)
expression, symbol
. with-out-
put-to-string .
string , with-output-to-string , -
, .
string , ; , -
, , -
vector-push-extend, with-output-to-string
.
(write-byte i stream)
i stream. i.
(write-char character &optional stream)
character stream.
(write-line string &optional stream &key start end)
write-string, .
(write-sequence proseq &optional stream &key start end)
proseq stream. -
start end , .
(write-string string &optional stream &key start end)
string stream. start
end , .
(yes-or-no-p &optional format &rest args)
y-or-n-p, yes
no .
(y-or-n-p &optional format &rest args)
(/) format ( "") *query-io*
, format args. -
y n, t nil -
. -
.

(copy-pprint-dispatch &optional pprint-dispatch)


pprint-
dispatch, *print-pprint-dispatch*. pprint-
dispatch nil, *print-pprint-dispatch*.
396 D.

(format dest format &rest args)


dest: , t (
*standard-output*), nil ( -
, ). nil, -
. format , -
, formatter. ,
dest args. , -
, ~,
, , -
: @,
. : ; ,
; V v, -
( , nil); #, -
. -
( ), -
.
.
:
~w, g, m, pA
princ, ,
( @) m ( 0)
p ( #\Space), p, -
g ( 1), ,
( )
w ( 0). :
() nil.
~w, g, m, pS
~A, , prin1.
~W
, write. : -
, @
.
~C
.
, , write-char. -
: ; :@ ,
. -
@ #\.
~n%
n ( 1) .
~n&
~%, ,
fresh-line.
397

~n|
n ( 1) .
~n~
n ( 1) ~s.
~r, w, p, c, iR
.
r ( 10) -
p ( #\Space), -
, ,
, w. : i ( -
3) c ( #\,).
@
.
, ~R -
, -
. 4 four,
: fourth, @ IV, :@ IIII.
~w, p, c, iD
. ~10, w, p, c, iR.
~w, p, c, iB
. ~2, w, p, c, iR.
~w, p, c, iO
. ~8, w, p,
c, iR.
~w, p, c, iX
.
~16, w, p, c, iR.
~w, d, s, x, pF
,
, s (
0) d ( , -
) . -
, . -
w, p (
#\Space), ,
w. (
, w, x,
.) w d, s . -
@ .
~w, d, e, s, x, p, mE
, ~F, ,
s ( 1)
398 D.

, d ( -
, ) e (
, ) .
m, .
~w, d, e, s, x, p, mG
, -
~F ~E .
~d, n, w, p$
. -
, -
, , n ( 1)
d ( -
2) . , , w (
0) ; ,
p ( #\Space). @ -
. -
: .
pprint-newline , -
: :linear, @ -
:miser, : :fill, :@ :mandatory.
~<prefix~;body~;suffix~:>
pprint-logial-block, ,
. prefix suffix :prefix (
:per-line-prefix, ~@;) :suffix, body
, . body
,
, pprint-pop. -
~^ pprint-exit-if-list-exhausted. -
prefix, body suffix ,
suffix ""; ,
prefix "". : prefix suffix
"(" ")" . -
@ -
. ~:@>,
:fill
body.
~nI
(pprint-indent :block n). : -
(pprint-indent :current n).
~/name/
name , ,
: ; ; , -
, : @ ,
, .
399

~m, nT
,
m ( 1),
, -
n ( 1) m. @
m , , n.
: (pprint-tab :section m n). @
(pprint-tab :section-relative m n).
~w, n, m, p<+text0~;...~;textn~>
, text
w , , , m ( 0) -
p ( #\Space), -
text. -
w, -
, n. :
; @ . ~^
.
text0 ~a,b:;, ~;, , text0,
, ,
b ( 72) a (
0) .
~n*
n ( 0) . -
: n . @ n- (-
) .
~i[text0~;...~; textn~]
, texti. i , -
. text
~:;, ~;,
text ( ). -
: text: -
, , -
. @ text:
, -
.
~n{text~}
format text -
, -
, . , -
, n- (
) .
~:} ~}, -
format, n 0. text ,
( ). ~^ -
400 D.

. :
, -
format. @ -
.
~?
format:
,
( ) format
. @
,
format.
~(text~)
text, -
: : -
; @
; :@ -
.
~P
(eql) 1, , -
s. : . @,
1, y, ies; :@ -
.
~a, b, c^
format ( , -
) :
;
0; ; -
(<= a b c).
~ , -
. @
, .
(formatter string)
, -
, format ,
, .
(pprint object &optional stream)
print, ,
.
(pprint-dispatch object &optional pprint-dispatch)
pprint-dispatch,
object . pprint-dispatch
, *print-pprint-dispatch*;
401

nil, .
object,
, print-object. -
,
.
(pprint-exit-if-list-exhausted)
pprint-logical-block. ,
, nil.
(pprint-fill stream object &optional colon at)
object stream, ,
*print-pretty* . colon
( ) , -
, . at -
. nil.
(pprint-indent keyword r &optional stream)
stream pprint-logical-block
*print-pretty* , -
. keyword :current, -
, r;
:block, ,
r.
(pprint-linear stream object &optional colon at)
pprint-fill, -
.
(pprint-logical-block (symbol object
&key prefix per-line-prefix suffix)
declaration* expression*)
expressions, symbol
( pprint-logical-block), -
symbol ( -
). , , -
. expression
.
object , -
expressions. ,
write; , -
pprint-pop. -
*print-circle* *print-level*.
, , .
prefix , suffix ,
per-line-prefix . prefix per-
line-prefix .
402 D.

(pprint-newline keyword &optional stream)


stream pprint-logical-block -
*print-pretty* , stream
keyword: :mandatory ; :linear
; :miser -
; :fill -
.
(pprint-pop)
pprint-logical-block. ,
, , -
. ( nil),
nil. *print-length* -
, -
nil. *print-circle* -
,
, #n#, nil.
(pprint-tab keyword i1 i2 &optional stream)
stream pprint-logical-block -
*print-pretty* , , -
~T
i1 i2. : :line
~T, :section ~:T, :line-relative ~@T, :section-relative ~:@T.
(pprint-tabular stream object &optional colon at tab)
pprint-fill, ,
. tab ( 16) -
.
(princ object &optional stream)
object stream -
( ). Escape- ( -
) .
(princ-to-string object)
princ, , -
.
(print object &optional stream)
prin1, .
(print-object object stream)
object stream.
(print-not-readable-object condition)
,
condition.
403

(print-unreadable-object (object stream &key type


identity) expression*)
#<...>. -
. #< stream; ,
type , ; -
expression,
object stream; , identity , -
; >. nil.
(prin1 object &optional stream)
object stream , (
) read.
(prin1-to-string object)
prin1, , -
.
(set-pprint-dispatch type function &optional r pprint-dispatch)
function , pprint-dispatch
( *print-pprint-dispatch*) type, -
function r ( 0). function
: .
function nil, type. -
nil.
(write object &key array base case circle escape gensym
length level lines miser-width pprint-dispatch
pretty radix readably right-margin stream)
object stream, -
*print-...*
.
(write-to-string object &key ...)
write, ,
.

(copy-readtable &optional from to)


to nil, (
*readtable*); to , -
from.
(get-dispatch-macro-character char1 char2
&optional readtable)
( nil, ), -
readtable, char1 char2.
404 D.

(get-macro-character char &optional readtable)


: ( nil, -
), readtable, char, -
, char
.
(make-dispatch-macro-character char &optional
nonterm readtable)
readtable char.
nonterm , char, ,
. t.
(read &optional stream eof-error eof-value recursive)
- stream, -
. ,
, eof-value eof-
error ( ). read -
recursive.
(read-delimited-list char &optional stream recursive)
read, -
stream , char,
. ,
char.
(read-from-string string &optional eof-error eof-value
&key start end preserve-whitespace)
string , read .
: (-
)
string. preserve-whitespace , re-
ad-preserving-whitespace read.
(read-preserving-whitespace &optional stream eof-error
eof-value recursive)
read, .
(readtable-case readtable)
: :upcase, :downcase, :preserve :in-
vert readtable -
. setf.
(readtablep object)
, object .
(set-dispatch-macro-character char1 char2 function
&optional readtable)
readtable , , function -
char1 char2 (
405

). func-
tion , char1
char2 , , -
. t.
(set-macro-character char function
&optional nonterm readtable)
readtable , , function -
char. function
char -
, . nonterm -
, char, , -
. t.
(set-syntax-from-char to-char from-char
to-readtable from-readtable)
to-char to-readtable ( *read-
table*) from-char from-
readtable ( ). t.
(with-standard-io-syntax expression*)
expression, -
, (. . *read-
*print-), .


(compile-file path &key output-file verbose print
external-format)
, path, -
output-file. verbose , -
*standard-output*. print , -
toplevel- *standard-output*. -
( ) external-format :default.
-
*readtable* *package*. :
( nil, ); , ,
-
; , , -
, .
(compile-file-pathname path &key output-file)
, compile-file, -
.
.
406 D.

(load path &key verbose print if-does-not-exist external-format)


, path. -
, tople-
vel-.
, -
compiled-function-p. verbose , -
*standard-output*.
print , *stan-
dard-output*. if-does-not-exist ( ),
;
nil. ( )
external-format :default. t, .
(provide name) []
name ( ) *modu-
les*.
(require name &optional paths) []
name ( )
*modules*, ,
. path, , ,
, .
(with-compilation-unit ([:override val]) expression*)
expression. , -
, -
. -
val.

(apropos name &optional package)


, -
name ( name, ).
package, .
.
(apropos-list name &optional package)
apropos, , -
.
(decode-universal-time i &optional time-zone)
i 1 1990 -
, 00:00:00 (GMT), : , ,
, , ( 1), , (-
0), -
, -
407

GMT. time-zone
-24 24 ; , -
.
(describe object &optional stream)
object stream.
.
(describe-object object &optional stream)
describe object stream.
(disassemble fn)
, fn,
, -.
(documentation object symbol)
object symbol nil,
. setf.
(dribble &optional path)
path,
-. , -
.
(ed &optional arg)
( ). arg
, .
, .
(encode-universal-time second minute hour date month year
&optional time-zone)
, decode-unversal-time.
(get-decoded-time)
(decode-universal-time (get-universal-time)).
(get-internal-real-time)
, -
internal-time-units-per-second .
(get-internal-run-time)
get-internal-real-time, ,
- -
.
(get-universal-time)
1 1900 -
, 00:00:00 (GMT).
408 D.

(inspect object)
describe, -
.
(lisp-implementation-type)
, ,
nil.
(lisp-implementation-version)
, -
, nil.
(long-site-name)
short-site-name, .
(machine-instance)
, , -
-, nil.
(machine-type)
, , -
-, nil.
(machine-version)
, , -
-, nil.
(room &optional (arg :default))
, . -
nil, -
t.
(short-site-name)
, -
, nil.
(sleep r)
r .
(software-type)
, -
, nil.
(software-version)
, -
, nil.
(step expression)
expression, -
(), .
409

(time expression)
expression, (),
*trace-output* .
(trace fname*)

*trace-output*. , -
inline-. , -
, .
(untrace fname*)
trace. , -
.
(user-homedir-pathname &optional host)
nil,
. host ,
make-pathname.


array-dimension-limit
fixnum,
. -
, 1024.
array-rank-limit
fixnum,
. ,
8.
array-total-size-limit
fixnum,
. ,
1024.
boole-1 ... boole-xor
,
boole.
*break-on-signals*
*break-on-warnings*.
.
, . nil.
410 D.

call-arguments-limit
,
. ,
50.
char-code-limit
,
, char-code. .
*compile-file-pathname*
compile-file , -
. nil.
*compile-file-truename*
*compile-file-pathname*.
*compile-print*
:print compile-
file. .
*compile-verbose*
:verbose com-
pile-file. .
*debug-io*
, .
*debugger-hook*
nil, f .
f -
f. f ,
. f *debugger-
hook* nil.
*default-pathname-defaults*
,
make-pathname :defaults.
short-float-epsilon
single-float-epsilon
double-float-epsilon
long-float-epsilon
-
,
1.0 , 1.0. -
.
411

short-float-negative-epsilon
single-float-negative-epsilon
double-float-negative-epsilon
long-float-negative-epsilon
-
, 1.0
, 1.0.
.
*error-output*
, .
*features*
, -
, . -
#+ #-.
*gensym-counter*
, gensym
. .
internal-time-units-per-second
get-internal-run-time
, -
.
lambda-list-keywords
- (-
, &optional, &rest . .), .
lambda-parameters-limit
,
-.
, 50.
least-negative-short-float
least-negative-single-float
least-negative-double-float
least-negative-long-float

, .
least-negative-normalized-short-float
least-negative-normalized-single-float
least-negative-normalized-double-float
least-negative-normalized-long-float
-
, .
412 D.

least-positive-short-float
least-positive-single-float
least-positive-double-float
least-positive-long-float

, .
least-positive-normalized-short-float
least-positive-normalized-single-float
least-positive-normalized-double-float
least-positive-normalized-long-float
-
, .
*load-pathname*
load , -
. nil.
*load-print*
:print
load. .
*load-truename*
*load-pathname*.
*load-verbose*
:verbose -
load. .
*macroexpand-hook*
,
macroexpand-1 -
. funcall
.
*modules*
, provide.
most-negative-fixnum
fixnum, -
.
most-negative-short-float
most-negative-single-float
most-negative-double-float
most-negative-long-float

, .
413

most-positive-fixnum
fixnum, -
.
most-positive-short-float
most-positive-single-float
most-positive-double-float
most-positive-long-float

, .
multiple-values-limit
,
. ,
20.
nil
. -
.
*package*
. common-lisp-user.
pi
long-float.
*print-array*
, (readable) -
. .
*print-base*
2 36 . -
, .
10 ( ).
*print-case*
,
. : :upcase ( ), :down-
case :capitalize ( ,
string-capitalize ).
*print-circle*
, -
#n= #n#. nil.
*print-escape*
nil, , princ.
t.
414 D.

*print-gensym*
, -
#:. t.
*print-length*
nil (), , -
, -
( ). nil, -
.
*print-level*
nil (), , -
-
( ). nil, .
*print-lines*
nil (), , -
, -
( ).
nil, .
*print-miser-width*
nil, , -
, ,
. .
*print-pprint-dispatch*
nil, . -
,
.
*print-pretty*
,
. .
*print-radix*
, -
. nil.
*print-readably*
, -
, . nil.
*print-right-margin*
nil (), , -
, .
nil, .
415

*query-io*
, .
*random-state*
,
Common Lisp.
*read-base*
2 36 .
, . 10
( ).
*read-default-float-format*
, read
. -
. single-float.
*read-eval*
nil, #. . t.
*read-suppress*
, read -
. nil.
*readtable*
.
Common Lisp.
*standard-input*
.
*standard-output*
.
t
. .
*terminal-io*
, ( ).
*trace-output*
, .
* ** ***
-
, toplevel.
+ ++ +++
,
toplevel.
416 D.

-
toplevel -
.
/ // ///
, -
, toplevel.


.
, (,
integer). -
. -
.
(and type*)
type.
(array type dimensions)
(simple-array type dimensions)
type ,
dimensions. dimensions
, ;
, ( -
make-array). simple-array -
. *, type
dimensions, -
.
(base-string i)
(simple-base-string i)
(vector base-character i) (simple-array base-character (i))
.
(bit-vector i)
(simple-bit-vector i)
(array bit (i)) (simple-array bit (i)) .
(complex type)
, -
type.
(cons type1 type2)
, car
type1, cdr type2. * t.
(eql object)
: object.
417

(float min max)


(short-float min max)
(single-float min max)
(double-float min max)
(long-float min max)
-
min max, min max f
( f), (f) ( f), f -
*, .
(function parameters type)
. -
, , parameters,
() type. parameters -
(-
(key type)). -
, &rest, -
, , -
. ( values.)
(integer min max)
float .
(member object*)
object.
(mod i)
, i.
(not type)
type.
(or type*)
type.
(rational min max)
float .
(real min max)
float .
(satisfies symbol)
, -
symbol.
(signed-byte i)
2(i1) 2(i1)1 -
. integer, i *.
418 D.

(string i)
(simple-string i)
i -
.
(unsigned-byte i)
,
2i. (integer 0 *), i *.
(values . parameters)
function the.
, -
multiple-value-call (function parameters).
(vector type i)
(simple-vector i)
(array type (i)) (simple-array t (i)) .
, -
.
.


, , (, ), , ; . -

#.

#\c c.
#f (function f).
#(...) .
#n(...) n . -
, -
.
#*bbb -.
#n*bbb - n .
,
.
#:sym sym.
#.expr expr .
#Bddd .
#Oddd .
#Xddd .
419

#nRddd n,
2 36 .
#C(a b) a+bi.
#nAexpr ,
expr :initial-contents make-array.
#S(sym ...) sym,

, .
#Pexpr (parse-namestring expr).
#n=expr expr,
n.
#n# , n .
#+test expr test , expr, .
#-test expr test , expr, -
.
#|...|# . .
#< .

, , -
, .
,
, -
. ,
( ) .
,
(b -
a c, a , b, b -
, c). ,
(well-formed) -
.
, x a, 1; y -
b, 2. :
(w ,x ,,y)
, -
.
. -
, :
(w ,x ,b)
420 D.

, :
(w a 2)
- (,@) , -
, - -
. ,
.
(w ,x ,,@(list a b))

(w ,x ,a ,b)
- (,.) -, -
.

. ,
, .
14 Steele, Guy L., Jr., with Scott E. Fahlman, Richard P. Gabriel, David A. Moon,
Daniel L. Weinreb, Daniel G. Bobrow, Linda G. DeMichiel, Sonya E. Keene, Gregor
Kiczales, Crispin Perdue, Kent M. Pitman, Richard C. Waters, and Jon L White.
Common Lisp: the Language, 2nd Edition. Digital Press, Bedford (MA), 1990.
19 McCarthy, John. Recursive Functions of Symbolic Expressions and their Com-
putation by Machine, Part I. CACM, 3:4 (April 1960), pp. 184195.
McCarthy, John. History of Lisp. In Wexelblat, Richard L. (Ed.) History of
Programming Languages. Academic Press, New York, 1981, pp. 173197.
http://www-formal.stanford.edu/jmc/.
21 Brooks, Frederick P. The Mythical Man-Month1. Addison-Wesley, Reading
(MA), 1975, p. 16.

. ,
.
. -
(, , ) -
, .
. -
.
22 , p. i.
23 Murray, Peter and Linda. The Art of the Renaissance. Thames and Hudson,
London, 1963, p. 85.
23 Janson, W J. History of Art, 3rd Edition. Abrams, New York, 1986, p. 374.
, ,
. -
. , -
, , -
.
30 car cdr
: car Contents of Address part of Register,

1
-, -
. . . .: -, 2000.
422

, cdr Contents of the Decrement


part of the Register, .
34 , -
, :
Touretzky, David S. Common Lisp: A Gentle Introduction to Symbolic
Computation. Benjamin/Cummings, Redwood City (CA), 1990, Chapter 8.
Friedman, Daniel P., and Matthias Felleisen. The Little Lisper. MIT Press,
Cambridge, 1987.
43 ANSI Common Lisp lambda,
(lambda (x) x) #(lambda (x) x).
- ( -
#),
.
44 Gabriel, Richard P. Lisp: Good News, Bad News, How to Win Big. AI Expert,
June 1991, p. 34.
62 sort: -
,
. , (2 1 1.0) < -
Common Lisp (1 1.0 2), (1.0 1 2).
-
, stable-sort ( -
), .
76
. . , ,
. -
. -
, .
77 ,
ASCII, Common Lisp : 26
, ,
, 0 9.
90
, . . Sedgewick, Robert. Algorithms. Addison-
Wesley, Reading (MA), 1988.
95 progn Common
Lisp (. 27). progn , -
:
(defun our-progn (&rest args)
(car (last args)))
, -
progn, .
98 - , -
, . ,
:
(let ((&key 1) (&optional 2)))
423

, - :
((lambda (&key &optional)) 1 2)
do labels . -
(David Kuzhnik) .
102 Steele, Guy L., Jr., and Richard P. Gabriel. The Evolution of Lisp. ACM SIGPLAN
Notices 28:3 (March 1993). , , -
Common Lisp Scheme.
104 , , -
:
(defun get-time-string ()
(multiple-value-bind (s m h) (get-decoded-time)
(format nil "~A:~2,,,O@A:~2,,,O@A" h m s)))
107 18 ( ) 1751 :
, , -
.
[ 1582];
, -
, , . ,
-
, .
, , , -
, . -
:
,
. : -
, -
. ,

. , -
, -
, . , -
, ,
. -
, ,
. -
, -
. ,
. ,
, -
. , , .
. Roberts, David (Ed.) Lord Chesterfields Letters. Oxford University Press,
Oxford, 1992.
108 Common Lisp , -
, 1900 . encode-
universal-time decode-universal-time -
. , 1900 Common Lisp -
:
424

(defun num->date (n)


(multiple-value-bind (ig no re d m y)
(decode-universal-time n)
(values d m y)))

(defun date->num (d m y)
(encode-universal-time 1 0 0 d m y))

(defun date+ (d m y n)
(num->date (+ (date->num d m y)
(* 60 60 24 n))))
, -
fixnum.
112 setf -
, . , , marble
color:
(defstruct marble
color)
(marble)
, , nil
:
(defun uniform-color (lst)
(let ((c (marble-color (car lst))))
(dolist (m (cdr lst))
(unless (eql (marble-color m) c)
(return nil)))
c))
uniform-color , -
setf .
(defun (setf uniform-color) (val lst)
(dolist (m lst)
(setf (marble-color m) val)))

(setf (uniform-color *marbles*) red)
*marbles* .
112 Common Lisp defsetf -
setf-.
, -
,
defsetf. , :
(defun (setf primo) (val lst) (setf (car lst) val))

(defsetf primo set-primo)

(defun set-primo (lst val) (setf (car lst) val))


425

118 , , ,
, , (. .
), ,
(. . apply). , -
.
map-int filter? , . -
, , , -
32- .
120 -
Abelson, Harold, and Gerald Jay Sussman, with Julie Sussman.
Structure and Interpretation of Computer Programs.1 MIT Press, Cambridge,
1985.
120 Dylan Shalit,
Andrew, with Kim Barrett, David Moon, Orca Starbuck, and Steve Strassmann.
Dylan Interim Reference Manual. Apple Computer, 1994. -
2 , http://www.harlequin.com http://
www.apple.com.
Scheme .
1975
Clinger, William, and Jonathan A. Rees (Eds.) Revised Report
on the Algorithmic Language Scheme3. 1991.
, Scheme
FTP: swiss-ftp.ai.mit.edu:pub.
Scheme: Structure and
Interpretation of Computer Programs ( -
), , Springer, George and Daniel P. Friedman
Scheme and the Art of Programming. MIT Press, Cambridge, 1989.
123 -
. Common Lisp,
.
, , , , -
, .
(. 176). -
, ,
. -

1
. ( -
;
SICP), , -
MIT . . .
2
, , -
. -
Dylan http://www.opendylan.org. .
.
3
-
(R6RS), http://www.r6rs.org. . .
426

, ,
.
, , mapcar:
(defun our-mapcar (fn x)
(if (null x)
nil
(cons (funcall fn (car x))
(our-mapcar fn (cdr x)))))
, add-to-all, -
:
(defun add-to-all (lst x)
(our-mapcar #(lambda (num) (+ num x))
lst))
Common Lisp , ,
, . ,
our-mapcar, x. our-
mapcar x , -
add-to-all. our-mapcar x
, , our-
mapcar. +,
.
134 Common Lisp *read-eval*, -
#.. read-from-string -
*read-eval* nil.
, #.
.
136 -
, -
. -
Sedgewick, Robert Algorithms.
Addison-Wesley, Reading (MA), 1988.
151 1984 Common Lisp reduce :key,
random-next :
(defun random-next (prev)
(let* ((choices (gethash prev *words*))
(i (random (let ((x 0))
(dolist (c choices)
(incf x (cdr c)))
x))))
(dolist (pair choices)
(if (minusp (decf i (cdr pair)))
(return (car pair))))))
151 , Henley, 1989 -
- . -
.
, .
427

, -
, -
?
-
.
, . , -
, ,
(artificial
stupidity). , , -
, Henley, -
- .
, - ,
, .

, .
, .
,
. ,
(-
). , ,
.1
-
: Low, Jackson M. Poetry, Chance, Silence,
Etc. In Hall, Donald (Ed.) Claims for Poetry. University of Michigan Press, Ann
Arbor, 1982.
Online Book Initiative -
, , . 2
FTP: ftp.std.com:obi.
Dissociated Press Emacs.
161 16 , -
:
(defun float-limits ()
(dolist (m (most least))
(dolist (s (positive negative))

1
, . -
. , -
. .
. , -
: -
, SCIgen
( MIT) -
-3,
( 2008 ). -
. . .
2
, , OBI, . .
.
428

(dolist (f (short single double long))


(let ((n (intern (string-upcase
(format nil "~A-~A-~A-float"
m s f)))))
(format t "~30A ~A~%" n (symbol-value n)))))))
174 quicksort 1962 -
Sorting and Searching. Addison-Wesley, Reading (MA), 1973.
182 Foderaro, John K. Introduction to the Special Lisp Section. CACM 34:9 (Sep-
tember 1991), p. 27.
185 CLOS :
Keene, Sonya E. Object Oriented Programming in Common Lisp. Addison-
Wesley, Reading (MA), 1989.
Kiczales, Gregor, Jim des Rivieres, and Daniel G. Bobrow. The Art of the
Metaobject Protocol. MIT Press, Cambridge, 1991.
187 :
. -
. -.
-
. -
-. , -
.
- --
, . ,
, -
, , : -
. , , -
, -
.
192 -
-
. :
1. :allocation, :initform ( ) :documentation ( )
.
2. :initarg :initarg -
. :accessor, :reader :writer.
3. :type :type .
199 ,
:
(progn
(defclass counter () ((#1=#:state :initform 0)))

(defmethod increment ((c counter))


(incf (slot-value c #1#)))

(defmethod clear ((c counter))


(setf (slot-value c #1#) 0)))
429

progn , ,
. ,
:
(defvar *symtab* (make-hash-table :test #equal))

(defun pseudo-intern (name)


(or (gethash name *symtab*)
(setf (gethash name *symtab*) (gensym))))

(set-dispatch-macro-character #\# #\[


#(lambda (stream charl char2)
(do ((acc nil (cons char acc))
(char (read-char stream) (read-char stream)))
((eql char #\]) (pseudo-intern acc)))))
:
(defclass counter () ((#[state] :initform 0)))

(defmethod increment ((c counter))


(incf (slot-value c #[state])))

(defmethod clear ((c counter))


(setf (slot-value c #[state]) 0))
210 :
(defmacro bst-push (obj bst <)
(multiple-value-bind (vars forms var set access)
(get-setf-expansion bst)
(let ((g (gensym)))
(let* ((,g ,obj)
,@(mapcar #list vars forms)
(,(car var) (bst-insert! ,g ,access ,<)))
,set))))
220 Knuth, Donald E. Structured Programming with goto Statements. Computing
Surveys, 6:4 (December 1974), pp. 261301.
221 Knuth, Donald E. Computer Programming as an Art. In ACM Turing Award
Lectures: The First Twenty Years. ACM Press, 1987.
, , : Knuth, Donald E. Literate
Programming. CSLI Lecture Notes #27, Stanford University Center for the Study
of Language and Information, Palo Alto, 1992.
223 Steele, Guy L., Jr. Debunking the "Expensive Procedure Call" Myth or, Proce-
dural Call Implementations Considered Harmful or, LAMBDA: The Ultimate
GOTO. Proceedings of the National Conference of the ACM, 1977, p. 157.
-
. , -
, ,
.
430

224 disassemble
Norvig, Peter Paradigms of Artificial Intelligence
Programming: Case Studies in Common Lisp. Morgan Kaufmann, San Mateo
(CA), 1992.
225 - -
C++,
. C++ , -
, .
-
. , -
C++, , -
. C++ , -
, -
.
226 . -
(, ) -
, -
. e
fixnum-, (with-type fixnum e).
(defmacro with-type (type expr)
(the ,type ,(if (atom expr)
expr
(expand-call type (binarize expr)))))

(defun expand-call (type expr)


(,(car expr) ,@(mapcar #(lambda (a)
(with-type ,type ,a))
(cdr expr))))

(defun binarize (expr)


(if (and (nthcdr 3 expr)
(member (car expr) (+ - * /)))
(destructing-bind (op a1 a2 . rest) expr
(binarize (,op (,op ,a1 ,a2) ,@rest)))
expr))
binarize , -
. :
(the fixnum (+ (the fixnum a)
(the fixnum b)
(the fixnum c)))
fixnum, -
(, a+b) fixnum.
with-type poly
(. 226) :
(defun poly (a b x)
(with-type fixnum (+ (* a (expt x 2)) (* b x))))
431

fixnum-, ,
, (with-type fixnum ...).
231 Unix- /usr/dict/
words.
233 T Scheme ,
.
Rees, Jonathan A., Norman I. Adams, and James R.Meehan. The T Manual, 5th
Edition. Yale University Computer Science Department, New Haven, 1988.
1 T,
FTP: hing.lcs.mit.edu:pub/t3.1.
237 -
, . , -
, - -
. , -
. -

. -
.
, ,
, -
, . -
13.7, : -
.
237 , , -
( ), -
. Cellini, Benvenuto. Autobiography. George Bull,
Penguin Books, Harmondsworth, 1956.
246 - -
. , ,
, ?
defmacro.
defmacro.
defmacro , , -
, -
. ,
.
, , . . -
, ,
.
, , -
. , -

1
. -
, ,
- . . .
432

,
.1
248 , loop -
, , -
. loop .
eval . -
loop , ,
, .
-
: loop- -
- , map-int, .
254 Russell,
Stuart, and Peter Norvig Artificial Intelligence: A Modern Approach2. Prentice
Hall, Englewood Cliffs (NJ), 1995.
278 17 setf -
defun. Common Lisp,
-
:
(proclaim (inline lookup set-lookup))

(defsetf lookup set-lookup)

(defun set-lookup (prop obj val)


(let ((off (position prop (layout obj) :test #eq)))
(if off
(setf (svref obj (+ off 3)) val)
(error "Cant set "A of ~A." val obj))))
(defmacro defprop (name &optional meth?)
(progn
(defun ,name (obj &rest args)
,(if meth?
(run-methods obj ,name args)
(rget ,name obj nil)))
(defsetf ,name (obj) (val)
(setf (lookup ,,name ,obj) ,val))))

1
, -
Common Lisp-
. . .
2
: ., . -
: , 2- . . . . . . .
.: , 2006.
( AIMA)
, . , -
,
- -
. Common Lisp, -
. : http://aima.cs.berke-
ley.edu/lisp/. . .
433

281 defmeth :
(defmacro defmeth (name obj parms &rest body)
(let ((gobj (gensym)))
(let ((,gobj ,obj))
(setf (gethash ,name ,gobj)
#(lambda ,parms
(labels ((next ()
(funcall (get-next ,gobj ,name)
,@parms)))
,@body))))))
next:
(defmeth area grumpy-circle (c)
(format t "How dare you stereotype me!~%")
(next))
. , next-method-p,
.
288 -
:
(defmacro with-slotref ((name prop class) &rest body)
(let ((g (gensym)))
(let ((,g (+ 3 (position ,prop (layout ,class)
:test #eq))))
(macrolet ((,name (obj) (svref ,obj ,,g)))
,@body))))
,
, . -
svref.
, , balloon:
(setf balloon-class (class nil size))
,
(balloons).
(defun popem (balloons)
(with-slotref (bsize size balloon-class)
(dolist (b balloons)
(setf (bsize b) 0))))
288 Gabriel, Richard P. Lisp: Good News, Bad News, How to Win Big. AI Expert,
June 1991, p. 35.
1973 ,
MacLisp PDP-10 ,
(. . DEC). . Fateman, Richard J. Reply
to an editorial. ACM SIGSAM Bulletin, 25 (March 1973), pp. 911.
419 . -
, ,x (bq (comma x)).
434

, eval, -
:
(defun eval2 (expr)
(case (and (consp expr) (car expr))
(comma (error "unmatched comma"))
(bq (eval-bq (second expr) 1))
(t (eval expr))))

(defun eval-bq (expr n)


(cond ((atom expr)
expr)
((eql (car expr) comma)
(if (= n 1)
(eval2 (second expr))
(list comma (eval-bq (second expr)
(1- n)))))
((eql (car expr) bq)
(list bq (eval-bq (second expr) (1+ n))))
(t
(cons (eval-bq (car expr) n)
(eval-bq (cdr expr) n)))))
n, eval-bq, -
. -
n, . , n = 1, -
.
. 419:
> (setf x a a 1 y b b 2)
2
> (eval2 (bq (bq (w (comma x) (comma (comma y))))))
(BQ (W (COMMA X) (COMMA B)))
> (eval2 *)
(W A 2)
435

.
.
,
.

(Richard Dawkins)
The Selfish Gene ( )


.
-
( -
). ,
, apply, -
.

(John McCarthy)
Recursive Functions of Symbolic Expressions
and their Computation by Machine, part I
(
, I)

#(, , 418
*, , 415 #*, , 418
*, , 365 #+, , 419
**, , 415 #<, , 419
***, , 415 #|, , 419
/, , 416
/, , 365
//, , 416 1, , 365
///, , 416 1+, , 365
+, , 415
+, , 365 A
++, , 415 abort, , 346
+++, , 415 abs, , 356
, , 416 acons, , 367
, , 365 acosh, , 356
/=, , 364 acos, , 356
<, , 365 add-method, , 335
<=, , 365 adjoin, , 368
=, , 364 adjustable-array-p, , 373
>, , 365 adjust-array, , 373
>=, , 365 allocate-instance, ,
#A, , 419 335
#B, , 418 alpha-char-p, , 365
#C, , 419 alphanumericp, , 365
#n#, , 419 and, , 321
#n=, , 419 append, , 368
#O, , 418 apply, , 321
#P, , 419 apropos-list, , 406
#R, , 419 apropos, , 406
#S, , 419 aref, , 374
#X, , 418 arithmetic-error-operands, , 356
;, , 418 arithmetic-error-operation, , 356
, , 418 array-dimension, , 374
, , 418 array-dimension-limit, , 409
(, , 418 array-dimensions, , 374
), , 418 array-displacement, , 374
# , 418 array-element-type, , 374
#, , 419 array-has-fill-pointer-p, , 374
#:, , 418 array-in-bounds-p, , 375
#., , 418 arrayp, , 375
#, , 418 array-rank-limit, , 409
437

array-rank, , 375 *compile-print*, , 410


C1

array-row-major-index, , 375 *compile-verbose*, , 410


C1

array-total-size-limit, , 409 call-arguments-limit, , 410


array-total-size, , 375 call-method, , 335
ash, , 357 call-next-method, , 335
asin, , 357 car, , 368
asinh, , 357 case, , 321
assert, , 346 catch, , 321
assoc, , 368 ccase, , 321
assoc-if, , 368 cdr, , 368
assoc-if-not, , 368 ceiling, , 358
atan, , 357 cell-error-name, , 347
atanh, , 357 cerror, , 347
atom, , 368 change-class, , 336
Autocad, 21 char, , 378
char-code, , 366
B char-code-limit, , 410
*break-on-signals*, , 409 char-downcase, , 366
char-equal, , 366
B

bit, , 375
bit-and, , 375 char-greaterp, , 366
bit-andc1, , 375 char-int, , 366
bit-andc2, , 375 char-lessp, , 366
bit-eqv, , 375 char-name, , 366
bit-ior, , 375 char-not-equal, , 366
bit-nand, , 375 char-not-greaterp, , 366
bit-nor, , 375 char-not-lessp, , 366
bit-not, , 375 char-upcase, , 366
bit-orc1, , 376 char/=, , 366
bit-orc2, , 376 char<, , 367
bit-vector-p, , 376 char<=, , 367
bit-xor, , 376 char=, , 366
block, , 321 char>, , 367
boole, , 357 char>=, , 367
boole-1, , 409 character, , 366
boole-2, , 409 characterp, , 366
boole-xor, , 409 check-type, , 347
both-case-p, , 365 cis, , 358
boundp, , 351 class-name, , 336
break, , 347 class-of, , 336
break loop, 28 clear-input, , 389
broadcast-stream-streams, , 389 clear-output, , 389
butlast, , 368 close, , 389
byte, , 358 clrhash, , 384
byte-position, , 358 code-char, , 367
byte-size, , 358 coerce, , 320
compile, , 316
C compile-file, , 405
compile-file-pathname, , 405
C, compiled-function-p, , 321
, 19 compiler-macro-function, , 317
, 27 complement, , 321
*compile-file-pathname*, ,
C1

complex, , 358
410 complexp, , 358
*compile-file-truename*, , 410
C1
438

compute-applicable-methods, defsetf, , 323


, 336 defstruct, , 344
compute-restarts, , 347 deftype, , 320
concatenate, , 380 defun, , 323
concatenated-stream-streams, , defvar, , 323
390 delete, , 382
cond, , 322 delete-duplicates, , 382
conjugate, , 358 delete-file, , 388
cons, , 368 delete-if, , 383
consp, , 369 delete-if-not, , 383
constantly, , 322 delete-package, , 353
constantp, , 317 denominator, , 358
continue, , 347 deposit-field, , 359
copy-alist, , 369 describe, , 407
copy-list, , 369 describe-object, ,
copy-pprint-dispatch, , 395 407
copy-readtable, , 403 destructing-bind, , 323
copy-seq, , 380 digit-char, , 367
copy-structure, , 344 digit-char-p, , 367
copy-symbol, , 351 directory, , 388
copy-tree, , 369 directory-namestring, , 386
cos, , 358 disassemble, , 407
cosh, , 358 do*, , 330
count, , 380 do, , 330
count-if, , 380 do-all-symbols, , 353
count-if-not, , 380 do-external-symbols, , 353
ctypecase, , 322 do-symbols, , 353
cxr, , 56, 368 documentation, ,
407
D dolist, , 330
*debug-io*, , 410 dotimes, , 331
double-float-epsilon, , 410
D

*debugger-hook*, , 410
double-float-negative-epsilon, ,
D

*default-pathname-defaults*, -
411
D

, 410
decf, , 358 dpb, , 359
declaim, , 317 dribble, , 407
declare, 317 dynamic-extent, , 317
decode-float, , 358
decode-universal-time, , 406 E
defclass, , 336 *error-output*, , 411
E1

defconstant, , 322 ecase, , 323


defgeneric, , 337 echo-stream-input-stream, , 390
define-compiler-macro, , 317 echo-stream-output-stream, ,
define-condition, , 347 390
define-method-combination, , 338, ed, , 407
339 elt, , 380
define-modify-macro, , 322 Emacs, 21, 35
define-setf-expander, , 322 encode-universal-time, , 407
define-symbol-macro, , 318 endp, , 369
defmacro, , 318 enough-namestring, , 386
defmethod, , 340 ensure-directories-exist, , 389
defpackage, , 352 ensure-generic-function, , 340
defparameter, , 322 eq, , 323
439

eql, , 323 ftruncate, , 360


equal, , 324 ftype, , 317
equalp, , 324 funcall, , 325
error, , 347 function, , 325
etypecase, , 324 function-keywords, -
eval, , 318 , 341
eval-when, , 318 function-lambda-expression, ,
evenp, , 359 325
every, , 324 functionp, , 325
exp, , 359
export, , 354 G
expt, , 359 *gensym-counter*, , 411
G1

gcd, , 360
F gensym, , 351
*features*, , 411
F1 gentemp, , 351
fboundp, , 324 get, , 351
fceiling, , 359 get-decoded-time, , 407
fdefinition, , 324 get-dispatch-macro-character, ,
ffloor, , 359 403
file-author, , 389 get-internal-real-time, , 407
file-error-pathname, , 389 get-internal-run-time, , 407
file-length, , 390 get-macro-character, , 404
file-namestring, , 386 get-output-stream-string, , 390
file-position, , 390 get-properties, , 369
file-string-length, , 390 get-setf-expansion, , 325
file-write-date, , 389 get-universal-time, , 407
fill, , 381 getf, , 369
fill-pointer, , 376 gethash, , 384
find, , 381 go, , 325
find-all-symbols, , 354 graphic-char-p, , 367
find-class, , 341
find-if, , 381 H
find-if-not, , 381 handler-bind, , 348
find-method, , 341 handler-case, , 348
find-package, , 354 hash-table-count, , 385
find-restart, , 347 hash-table-p, , 385
find-symbol, , 354 hash-table-rehash-size, , 385
finish-output, , 390 hash-table-rehash-threshold, ,
first, , 369 385
flet, , 324 hash-table-size, , 385
float, , 359 hash-table-test, , 385
float-digits, , 359 host-namestring, , 386
float-precision, , 359
float-radix, , 359 I
float-sign, , 359
floatp, , 359 identity, , 326
floor, , 360 if, , 326
fmakunbound, , 325 ignorable, , 317
force-output, , 390 ignore, , 317
format, , 396 ignore-errors, , 348
formatter, , 400 imagpart, , 360
fresh-line, , 390 import, , 354
fround, , 360 in-package, , 354
incf, , 360
440

initialize-instance, - least-positive-double-float, ,
, 341 412
inline, , 317 least-positive-long-float, , 412
input-stream-p, , 390 least-positive-normalized-double-float,
inspect, , 408 , 412
integer-decode-float, , 360 least-positive-normalized-long-float,
integer-length, , 360 , 412
integerp, , 360 least-positive-normalized-short-float,
interactive-stream-p, , 391 , 412
Interleaf, 21 least-positive-normalized-single-float,
intern, , 354 , 412
internal-time-units-per-second, - least-positive-short-float, , 412
, 411 least-positive-single-float, , 412
intersection, , 369 length, , 381
invalid-method-error, , 348 let*, , 326
invoke-debugger, , 348 let, , 326
invoke-restart, , 348 lisp-implementation-type, , 408
invoke-restart-interactively, , lisp-implementation-version, ,
348 408
isqrt, , 360 list*, , 370
list, , 369
K list-all-packages, , 354
keywordp, , 351 list-length, , 370
listen, , 391
L listp, , 370
load, , 406
*load-pathname*, , 412
L1

load-logical-pathname-translations,
*load-print*, , 412
L1

, 386
*load-truename*, , 412
L1

load-time-value, ,
*load-verbose*, , 412
L1

318
labels, , 325 locally, , 318
lambda, , 318 log, , 361
lambda-list-keywords, , 411 logand, , 361
lambda-parameters-limit, , 411 logandc1, , 361
last, , 369 logandc2, , 361
lcm, , 360 logbitp, , 361
ldb, , 360 logcount, , 361
ldb-test, , 360 logeqv, , 361
ldiff, , 369 logical-pathname, , 386
least-negative-double-float, , logical-pathname-translations, ,
411 386
least-negative-long-float, , 411 logior, , 361
least-negative-normalized-double-float, lognand, , 361
, 411 lognor, , 361
least-negative-normalized-long-float, lognot, , 361
, 411 logorc1, , 362
least-negative-normalized-short-float, logorc2, , 362
, 411 logtest, , 362
least-negative-normalized-single-float, logxor, , 362
, 411 long-float-epsilon, , 410
least-negative-short-float, , 411 long-float-negative-epsilon, ,
least-negative-single-float, , 411
411 long-site-name, , 408
loop, , 331
441

loop-finish, , 335 member, , 371


lower-case-p, , 367 member-if, , 371
member-if-not, , 371
M merge, , 381
*macroexpand-hook*, , 412 merge-pathnames, , 387
method-combination-error, , 349
M1

*modules*, , 412
method-qualifiers, ,
M1

machine-instance, , 408
machine-type, , 408 342
machine-version, , 408 min, , 362
macro-function, , 319 minusp, , 362
macroexpand, , 319 mismatch, , 381
macroexpand-1, , 319 mod, , 362
macrolet, , 326 most-negative-double-float, ,
make-array, , 376 412
make-broadcast-stream, , 391 most-negative-fixnum, , 412
make-concatenated-stream, , 391 most-negative-long-float, , 412
make-condition, , 349 most-negative-short-float, , 412
make-dispatch-macro-character, - most-negative-single-float, ,
, 404 412
make-echo-stream, , 391 most-positive-double-float, ,
make-hash-table, , 385 413
make-instance, , most-positive-fixnum, , 413
341 most-positive-long-float, , 413
make-instance-obsolete, most-positive-short-float, , 413
, 341 most-positive-single-float, , 413
make-list, , 370 muffle-warning, , 349
make-load-form, , multiple-value-bind, , 326
342 multiple-value-call,
make-load-form-saving-slots, , , 326
342 multiple-value-list, , 326
make-package, , 354 multiple-value-prog1,
make-pathname, , 386 , 326
make-random-state, , 362 multiple-value-setq, , 326
make-sequence, , 381 multiple-values-limit, , 413
make-string, , 378
make-string-input-stream, , 391 N
make-string-output-stream, , name-char, , 367
391 namestring, , 387
make-symbol, , 351 nbutlast, , 368
make-synonym-stream, , 391 nconc, , 371
make-two-way-stream, , 391 next-method-p, , 342
makunbound, , 351 nil, , 413
map, , 381 nintersection, , 369
map-into, , 381 no-applicable-method,
mapc, , 370 , 342
mapcan, , 370 no-next-method, ,
mapcar, , 370 342
mapcon, , 370 not, , 327
maphash, , 385 notany, , 327
mapl, , 370 notevery, , 327
maplist, , 370 notinline, , 317
mask-field, , 362 nreconc, , 372
max, , 362 nreverse, , 383
442

nset-difference, , 372 packagep, , 355


nset-exclusive-or, , 372 pairlis, , 371
nstring-capitalize, , 378 parse-integer, , 363
nstring-downcase, , 378 parse-namestring, , 387
nstring-upcase, , 379 pathname, , 387
nsublis, , 372 pathname-device, , 388
nsubst, , 373 pathname-directory, , 388
nsubst-if, , 373 pathname-host, , 387
nsubst-if-not, , 373 pathname-match-p, , 388
nsubstitute, , 384 pathname-name, , 388
nsubstitute-if, , 384 pathname-type, , 388
nsubstitute-if-not, , 384 pathname-version, , 388
nth, , 371 pathnamep, , 388
nth-value, , 327 peek-char, , 392
nthcdr, , 371 phase, , 363
null, , 371 pi, , 413
numberp, , 362 plusp, , 363
numerator, , 362 pop, , 371
nunion, , 373 position, , 382
position-if, , 382
O position-if-not, , 382
oddp, , 362 pprint, , 400
open, , 391 pprint-dispatch, , 400
open-stream-p, , 392 pprint-exit-if-list-exhausted, ,
optimize, , 317 401
or, , 327 pprint-fill, , 401
OS/360, 22 pprint-indent, , 401
output-stream-p, , 392 pprint-linear, , 401
pprint-logical-block, , 401
P pprint-newline, , 402
pprint-pop, , 402
*package*, , 413
P1

pprint-tab, , 402
*print-array*, , 413
P1

pprint-tabular, , 402
*print-base*, , 413
P1

prin1, , 403
*print-case*, , 413
P1

prin1-to-string, , 403
*print-circle*, , 413
P1

princ, , 402
*print-escape*, , 413
P1

princ-to-string, , 402
*print-gensym*, , 414
P1

print, , 402
*print-length*, , 414
P1

print-not-readable-object, , 402
*print-level*, , 414
P1

print-object, , 402
*print-lines*, , 414
P1

print-unreadable-object, , 403
*print-miser-width*, , 414
P1

probe-file, , 389
*print-pprint-dispatch*, , 414
P1

proclaim, , 319
*print-pretty*, , 414
P1

prog*, , 327
*print-radix*, , 414
P1

prog, , 327
*print-readably*, , 414
P1

prog1, , 327
*print-right-margin*, , 414
P1

prog2, , 327
package-error-package, , 355 progn, , 327
package-name, , 355 progv, , 327
package-nicknames, , 355 provide, , 406
package-shadowing-symbols, , psetf, , 328
355 psetq, , 328
package-use-list, , 355 push, , 371
package-used-by-list, , 355 pushnew, , 371
443

Q restart-case, , 349
query-io*, , 415 restart-name, , 350
quote, , 320 return, , 328
return-from, , 328
revappend, , 372
R reverse, , 383
*random-state*, , 415
R1

room, , 408
*read-base*, , 415
R1

rotatef, , 328
*read-default-float-format*, ,
R1

round, , 364
415 row-major-aref, , 377
*read-eval*, , 415
R1

rplaca, , 372
*read-suppress*, , 415
R1

rplacd, , 372
*readtable*, , 415
R1

random, , 363 S
random-state-p, , 363
rassoc, , 371 *standard-input*, , 415
S1

rassoc-if, , 372 *standard-output*, , 415


S1

rassoc-if-not, , 372 sbit, , 377


rational, , 363 scale-float, , 364
rationalize, , 363 schar, , 378
rationalp, , 363 search, , 383
read, , 404 second, , 369
read-byte, , 393 set, , 351
read-char, , 393 set-difference, , 372
read-char-no-hang, , 393 set-dispatch-macro-character, ,
read-delimited-list, , 404 404
read-from-string, , 404 set-exclusive-or, , 372
read-line, , 393 set-macro-character, , 405
read-preserving-whitespace, , set-pprint-dispatch, , 403
404 set-syntax-from-char, , 405
read-sequence, , 393 setf, , 328
readtable-case, , 404 setq, , 328
readtablep, , 404 shadow, , 355
realp, , 363 shadowing-import, , 355
realpart, , 363 shared-initialize, ,
reduce, , 382 342
reinitialize-instance, shiftf, , 329
, 342 short-float-epsilon, , 410
rem, , 364 short-float-negative-epsilon, ,
remf, , 372 411
remhash, , 385 short-site-name, , 408
remove, , 382 signal, , 350
remove-duplicates, , 382 signum, , 364
remove-if, , 383 simple-bit-vector-p, , 377
remove-if-not, , 383 simple-condition-format-arguments,
remove-method, , , 350
342 simple-condition-format-control, -
remprop, , 352 , 350
rename-file, , 389 simple-string-p, , 378
rename-package, , 355 simple-vector-p, , 377
replace, , 383 sin, , 364
require, , 406 single-float-epsilon, , 410
rest, , 372 single-float-negative-epsilon, ,
restart-bind, , 349 411
444

sinh, , 364 sxhash, , 385


sleep, , 408 symbol-function, , 351
slot-boundp, , 343 symbol-macrolet, ,
slot-exists-p, , 343 319
slot-makunbound, , 343 symbol-name, , 352
slot-missing, , 343 symbol-package, , 352
slot-unbound, , 343 symbol-plist, , 352
slot-value, , 343 symbol-value, , 352
software-type, , 408 symbolp, , 352
software-version, , 408 synonym-stream-symbol, , 394
some, , 329
sort, , 383 T
special, , 317 t, , 415
special-operator-p, , 319 *terminal-io*, , 415
sqrt, , 364
T1

*trace-output*, , 415
stable-sort, , 383
T1

tagbody, , 329
standard-char-p, , 367 tailp, , 373
step, , 408 tan, , 364
store-value, , 350 tanh, , 364
stream-element-type, , 393 tenth, , 369
stream-error-stream, , 393 terpri, , 394
stream-external-format, , 393 the, , 319
streamp, , 393 throw, , 329
string, , 378 time, , 409
string-capitalize, , 378 toplevel, 25
string-downcase, , 378 trace, , 409
string-equal, , 378 translate-logical-pathname, ,
string-greaterp, , 379 388
string-left-trim, , 379 translate-pathname, , 388
string-lessp, , 379 tree-equal, , 373
string-not-equal, , 379 truename, , 389
string-not-greaterp, , 379 truncate, , 364
string-not-lessp, , 379 two-way-stream-input-stream, ,
string-right-trim, , 379 394
string-trim, , 379 two-way-stream-output-stream, ,
string-upcase, , 379 394
string/=, , 379 type, , 317
string<, , 380 type-error-datum, , 320
string<=, , 380 type-error-expected-type, , 320
string=, , 379 type-of, , 320
string>, , 380 typecase, , 329
string>=, , 380 typep, , 320
stringp, , 379
sublis, , 372 U
subseq, , 384
subsetp, , 372 unbound-slot-instance, , 343
subst, , 373 unexport, , 355
subst-if, , 373 unintern, , 356
subst-if-not, , 373 union, , 373
substitute, , 384 unless, , 329
substitute-if, , 384 unread-char, , 394
substitute-if-not, , 384 untrace, , 409
subtypep, , 320 unuse-package, , 356
svref, , 377
445

unwind-protect, , Z
329 zerop, , 364
update-instance-for-different-class,
, 344
update-instance-for-redefined-class,

, 344
upgraded-array-element-type, , , 75
377 , 174
upgraded-complex-part-type, , , 53
364 , 253
upper-case-p, , 367
use-package, , 356 , 254
use-value, , 350 , 68
user-homedir-pathname, , 409 , 67
, 254
V , 26
values, , 330
values-list, , 330

vector, , 377 , 57
vector-pop, , 377 , , 22
vector-push, , 377 , , 137
vector-push-extend, , 378
vectorp, , 377
vi, , 35 , 274
, 25
W , 26
warn, , 350 , 27
when, , 330 , 25
wild-pathname-p, , 388 , 27
with-accessors, , 343
with-compilation-unit, , 406
with-condition-restarts, , 350 , 149
with-hash-table-iterator, , 386
with-input-from-string, , 394
with-open-file, , 394

with-open-stream, , 394
(), 58
with-output-to-string, , 395
, 85
with-package-iterator, , 356
, 85
with-simple-restart, , 350
with-slots, , 343
with-standard-io-syntax, , 405

write, , 403 , 119
write-byte, , 395
write-char, , 395 , 35, 103
write-line, , 395 , 38
write-sequence, , 395 , 51
write-string, , 395
write-to-string, , 403
, 198
Y , 26
y-or-n-p, , 395 , 30
yes-or-no-p, , 395 , 40
446


, , 173 , 186, 275
, 187 , 276
, 191
, 192
, , 16, 220
, 206
, 35 , 32, 259
, 35 , 31, 171
, 118 , 31, 99
, 428 , 105
, 34 , 206
, 29, 84, 170
, 197
, 197
, 59 , 62
, 38
, 69
, 97, 118 , 70
, 422 , 69, 229
, 69
, 69
, 70

, 19 , 114
, 154 , 114
, 27 , 113
, 25, 33, 117, 223 , 60, 114
, 29 , 32
, 19 , 37, 123
, 253 , 37, 123
, 28 , 119
, 39, 111, , 123
116, 258, 279 , 82
, 31 , 36, 39
-, 43 , 190
, 26, 279

, , 19, 435 , 23
, 241 -, 115
, 141 -, 21, 115, 118, 263
, 141 , 22
, 141 , 39
, 221
, 227 , 232
, 59
, 186
, 195
, 195 , 59, 293
, 195 , 58, 59
, 193 , 34, 58, 126
, 194 , 127, 222
447

, 118
, 29 , 112
, 245 , 32
, 147 , 56
, 32 , 32
, 149 , 33
, 148 , 32
, 176 , 115
, 148, 244
, 187
, 29, 48 , 187
, 66
, 49
, 50 , 48
, 212
, 170
, 60
, 68
, 61
, 63
, 65
, 190, 277
, 29
, 145
, 203
, 30
, 215
, 216
, 232
, 36

, 44
, 239
, 44
, 44, 239
, 239
, 240
, 154

, 225
, 224
, 65

, 111
, 62, 70, 208
, 32
, 32
, 34
, 118
, 34

1995

.

,
.
: OReilly, Pearson Education, NewRiders,
Addison Wesley, Wiley, McGrawHill, No Starch Press, Packt, Dorset
House, Apress .


, , .
:

:

. . 16 , . 7 (. ),
. (812) 3805007
:

. 2 , . 14
(. /),
. (495) 6385305

http://www.symbol.ru


,
.
, , www.symbol.ru.

www. s ym bo l. ru . !