forked from filonenko-mikhail/cltl2-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.tex
5844 lines (5170 loc) · 269 KB
/
io.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%Part{Io, Root = "CLM.MSS"}
%%%Chapter of Common Lisp Manual. Copyright 1984, 1988, 1989 Guy L. Steele Jr.
\clearpage\def\pagestatus{FINAL PROOF}
\chapter{Input/Output}
\label{IO}
%\chapter{Система ввода/вывода}
%\label{IO}
Common Lisp provides a rich set of facilities for performing input/output.
All input/output operations are performed on streams of various kinds.
This chapter is devoted to stream data transfer operations.
Streams are discussed in chapter~\ref{STREAM}, and
ways of manipulating files through streams are discussed in
chapter~\ref{FILES}.
While there is provision for reading and writing binary data,
most of the I/O operations in Common Lisp read or write characters.
There are simple primitives for reading and writing single characters
or lines of data. The \cdf{format} function can perform complex
formatting of output data, directed by a control string
in manner similar to a Fortran \cdf{FORMAT} statement
or a \cd{PL/I} \cd{PUT EDIT} statement. The most useful I/O operations,
however, read and write printed representations of arbitrary
Lisp objects.
\section{Printed Representation of Lisp Objects}
Lisp objects in general are not text strings but complex data structures.
They have very different properties from text strings as a consequence of
their internal representation. However, to make it possible to get at
and talk about Lisp objects, Lisp provides a representation of
most objects in the form of printed text; this is called the \emph{printed
representation}, which is used for input/output purposes and in the
examples throughout this book. Functions such as \cdf{print} take a
Lisp object and send the characters of its printed representation to a
stream. The collection of routines that does this is known as the
(Lisp) \emph{printer}. The \cdf{read} function takes characters from a
stream, interprets them as a printed representation of a Lisp object,
builds that object, and returns it; the collection of routines
that does this is called the (Lisp) \emph{reader}.
\indexterm{printer}
\indexterm{printed representation}
\indexterm{reader}
\indexterm{printer}
\indexterm{printed representation}
\indexterm{reader}
Ideally, one could print a Lisp object and
then read the printed representation back in, and so obtain the same identical
object. In practice this is difficult and for some purposes not even desirable.
Instead, reading a printed representation produces an object that is (with
obscure technical exceptions) \cdf{equal} to the originally printed object.
Most Lisp objects have more than one possible printed representation.
For example, the integer twenty-seven can be written in any of these ways:
\begin{lisp}
27~~~~27.~~~~\#o33~~~~\#x1B~~~~\#b11011~~~~\#.(* 3 3 3)~~~~81/3
\end{lisp}
A list of two symbols \cdf{A} and \cdf{B} can be printed in many ways:
\begin{lisp}
~~~~(A B)~~~~(a b)~~~~(~~a~~b~)~~~~({\Xbackslash}A |B|) \\
~~~~(|{\Xbackslash}A| \\
~~B \\
)
\end{lisp}
The last example, which is spread over three lines, may be ugly, but it
is legitimate. In general, wherever whitespace is permissible in a printed
representation, any number of spaces and newlines may appear.
When \cdf{print} produces a printed representation, it must choose arbitrarily
from among many possible printed representations. It attempts to choose
one that is readable. There are a number of global variables that can
be used to control the actions of \cdf{print}, and a number of different
printing functions.
This section describes in detail what is the standard printed
representation for any Lisp object and also describes how \cdf{read} operates.
\subsection{What the Read Function Accepts}
\label{READER}
\indexterm{reader}
The purpose of the Lisp reader is to accept characters, interpret them
as the printed representation of a Lisp object, and construct and
return such an object. The reader cannot accept everything that the
printer produces; for example, the printed representations of compiled
code objects cannot be read in. However, the reader has
many features that are not used by the output of the printer at all,
such as comments, alternative representations, and convenient
abbreviations for frequently used but unwieldy constructs. The reader is
also parameterized in such a way that it can be used as a lexical
analyzer for a more general user-written parser.
The reader is organized as a recursive-descent parser.
Broadly speaking,
the reader operates by reading a character from
the input stream and treating it in one of three ways.
Whitespace characters serve as separators but are otherwise
ignored. Constituent and escape characters are accumulated
to make a \emph{token}, which is then interpreted as a number or symbol.
Macro characters trigger the invocation of functions (possibly
user-supplied) that can perform arbitrary parsing actions,
including recursive invocation of the reader.
More precisely,
when the reader is invoked, it reads a single character from the input stream
and dispatches according to the syntactic type of that character.
Every character that can appear in the input stream
must be of exactly one of the following kinds:
\emph{illegal},
\emph{whitespace},
\emph{constituent},
\emph{single escape},
\emph{multiple escape}, or
\emph{macro}.
Macro characters are further divided
into the types \emph{terminating} and \emph{non-terminating} (of tokens).
(Note that macro characters have nothing whatever to do with macros
in their operation. There is a superficial similarity in that macros allow
the user to extend the syntax of Common Lisp at the level of forms,
while macro characters allow the user to extend the syntax at the
level of characters.)
Constituents additionally have one or more attributes,
the most important of which is \emph{alphabetic}; these attributes are discussed
further in section~\ref{PARSE-TOKENS-SECTION}.
The parsing of Common Lisp expressions is discussed in terms of these
syntactic character types because the types of individual characters
are not fixed
but may be altered by the user (see \cdf{set-syntax-from-char}
and \cdf{set-macro-character}).
The characters of the standard character set initially have the
syntactic types shown in table~\ref{Standard-Character-Syntax-Table}.
Note that
the brackets, braces, question mark, and exclamation point
(that is, \cd{{\Xlbracket}}, \cd{{\Xrbracket}}, \cd{{\Xlbrace}},
\cd{{\Xrbrace}}, \cd{?},
and \cd{!}) are normally defined to be constituents, but they
are not used for any purpose in standard Common Lisp syntax and do not occur
in the names of built-in Common Lisp functions or variables.
These characters are explicitly reserved to the user.
The primary intent
is that they be used as macro characters; but a user might choose,
for example, to make \cd{!} be a \emph{single escape} character
(as it is in Portable Standard Lisp).
\begin{table}
\caption{Standard Character Syntax Types}
\label{Standard-Character-Syntax-Table}
\begin{tabular*}{\textwidth}{@{}l@{\extracolsep{\fill}}ll@{}}
$\langle$tab$\rangle$\cd{~~}\emph{whitespace}&$\langle$page$\rangle$\cd{~~}\emph{whitespace}&$\langle$newline$\rangle$\cd{~~}\emph{whitespace} \\
$\langle$space$\rangle$\cd{~~}\emph{whitespace}&\cd{{\Xatsign}~~}\emph{constituent}&\cd{{\Xbq}~~}\emph{terminating macro} \\
\cd{!~~}\emph{constituent} *&\cd{A~~}\emph{constituent}&\cd{a~~}\emph{constituent} \\
\cd{"~~}\emph{terminating macro}&\cd{B~~}\emph{constituent}&\cd{b~~}\emph{constituent} \\
\cd{\#~~}\emph{non-terminating macro}&\cd{C~~}\emph{constituent}&\cd{c~~}\emph{constituent} \\
\cd{\$~~}\emph{constituent}&\cd{D~~}\emph{constituent}&\cd{d~~}\emph{constituent} \\
\cd{\%~~}\emph{constituent}&\cd{E~~}\emph{constituent}&\cd{e~~}\emph{constituent} \\
\cd{\&~~}\emph{constituent}&\cd{F~~}\emph{constituent}&\cd{f~~}\emph{constituent} \\
\cd{'~~}\emph{terminating macro}&\cd{G~~}\emph{constituent}&\cd{g~~}\emph{constituent} \\
\cd{(~~}\emph{terminating macro}&\cd{H~~}\emph{constituent}&\cd{h~~}\emph{constituent} \\
\cd{)~~}\emph{terminating macro}&\cd{I~~}\emph{constituent}&\cd{i~~}\emph{constituent} \\
\cd{*~~}\emph{constituent}&\cd{J~~}\emph{constituent}&\cd{j~~}\emph{constituent} \\
\cd{+~~}\emph{constituent}&\cd{K~~}\emph{constituent}&\cd{k~~}\emph{constituent} \\
\cd{,~~}\emph{terminating macro}&\cd{L~~}\emph{constituent}&\cd{l~~}\emph{constituent} \\
\cd{-~~}\emph{constituent}&\cd{M~~}\emph{constituent}&\cd{m~~}\emph{constituent} \\
\cd{.~~}\emph{constituent}&\cd{N~~}\emph{constituent}&\cd{n~~}\emph{constituent} \\
\cd{/~~}\emph{constituent}&\cd{O~~}\emph{constituent}&\cd{o~~}\emph{constituent} \\
\cd{0~~}\emph{constituent}&\cd{P~~}\emph{constituent}&\cd{p~~}\emph{constituent} \\
\cd{1~~}\emph{constituent}&\cd{Q~~}\emph{constituent}&\cd{q~~}\emph{constituent} \\
\cd{2~~}\emph{constituent}&\cd{R~~}\emph{constituent}&\cd{r~~}\emph{constituent} \\
\cd{3~~}\emph{constituent}&\cd{S~~}\emph{constituent}&\cd{s~~}\emph{constituent} \\
\cd{4~~}\emph{constituent}&\cd{T~~}\emph{constituent}&\cd{t~~}\emph{constituent} \\
\cd{5~~}\emph{constituent}&\cd{U~~}\emph{constituent}&\cd{u~~}\emph{constituent} \\
\cd{6~~}\emph{constituent}&\cd{V~~}\emph{constituent}&\cd{v~~}\emph{constituent} \\
\cd{7~~}\emph{constituent}&\cd{W~~}\emph{constituent}&\cd{w~~}\emph{constituent} \\
\cd{8~~}\emph{constituent}&\cd{X~~}\emph{constituent}&\cd{x~~}\emph{constituent} \\
\cd{9~~}\emph{constituent}&\cd{Y~~}\emph{constituent}&\cd{y~~}\emph{constituent} \\
\cd{:~~}\emph{constituent}&\cd{Z~~}\emph{constituent}&\cd{z~~}\emph{constituent} \\
\cd{;~~}\emph{terminating macro}&\cd{{\Xlbracket}~~}\emph{constituent} *&\cd{{\Xlbrace}~~}\emph{constituent} * \\
\cd{<~~}\emph{constituent}&\cd{{\Xbackslash}~~}\emph{single escape}&\cd{|~~}\emph{multiple escape} \\
\cd{=~~}\emph{constituent}&\cd{{\Xrbracket}~~}\emph{constituent} *&\cd{{\Xrbrace}~~}\emph{constituent} * \\
\cd{>~~}\emph{constituent}&\cd{{\Xcircumflex}~~}\emph{constituent}&\cd{{\Xtilde}~~}\emph{constituent} \\
\cd{?~~}\emph{constituent} *&\cd{{\Xunderscore}~~}\emph{constituent}&$\langle$rubout$\rangle$\cd{~~}\emph{constituent} \\
$\langle$backspace$\rangle$\cd{~~}\emph{constituent}&$\langle$return$\rangle$\cd{~~}\emph{whitespace}&$\langle$linefeed$\rangle$\cd{~~}\emph{whitespace}
\end{tabular*}
\vfill
\begin{small}
\noindent
The characters marked with an asterisk are initially constituents
but are reserved to the user for use as macro characters or for
any other desired purpose.
\end{small}
\end{table}
The algorithm performed by the Common Lisp reader is roughly as follows:
\begingroup\leftmargini 1.5em
\begin{enumerate}
\item
If at end of file, perform end-of-file processing (as specified
by the caller of the \cdf{read} function).
Otherwise,
read one character from the input stream, call it \emph{x}, and
dispatch according to the syntactic type of \emph{x} to one
of steps~\ref{READER-ILLEGAL} to~\ref{READER-CONSTITUENT}.
\label{READER-START}
\item
If \emph{x} is an \emph{illegal} character, signal an error.
\label{READER-ILLEGAL}
\item
If \emph{x} is a \emph{whitespace} character,
then discard it and go back to step~\ref{READER-START}.
\label{READER-WHITESPACE}
\item
If \emph{x} is a \emph{macro} character (at this point the
distinction between \emph{terminating} and \emph{non-terminating} macro characters
does not matter), then execute the function associated
with that character. The function may return zero values or one value
(see \cdf{values}).
The macro-character function may of course read characters from the input
stream; if it does, it will see those characters following the macro
character. The function may even invoke the reader recursively.
This is how the macro character \cd{(} constructs a list:
by invoking the reader recursively to read the elements of the list.
If one value is returned, then return that value as the result of the
read operation; the algorithm is done.
If zero values are returned, then go back to step~\ref{READER-START}.
\item
If \emph{x} is a \emph{single escape} character (normally \cd{{\Xbackslash}}),
then read the next character and call it \emph{y}
(but if at end of file, signal an error instead).
Ignore the usual syntax of \emph{y}
and pretend it is a \emph{constituent} whose only attribute is
\emph{alphabetic}.
For the purposes of \cdf{readtable-case}, \emph{y} is not replaceable.
Use \emph{y} to begin a token, and go to step~\ref{READER-PLAIN-TOKEN}.
\item
If \emph{x} is a \emph{multiple escape} character (normally \cd{|}),
then begin a token (initially
containing no characters) and go to step~\ref{READER-MULTI-TOKEN}.
\item
If \emph{x} is a \emph{constituent} character, then it begins an extended token.
\label{READER-CONSTITUENT}\relax
After the entire token is read in, it will be interpreted
either as representing a Lisp object such as a symbol or number
(in which case that object is returned as the result of the read operation),
or as being of illegal syntax (in which case an error is signaled).
The case of \emph{x\/} should not be altered; instead,
\emph{x} should be regarded as replaceable.
Use \emph{x} to begin a token, and go on to step~\ref{READER-PLAIN-TOKEN}.
\item
(At this point a token is being accumulated, and an even number
of \emph{multiple escape} characters have been encountered.)
If at end of file, go to step~\ref{READER-TOKEN-END}.
Otherwise, read a character (call it \emph{y}), and
perform one of the following actions according to its syntactic type:
\label{READER-PLAIN-TOKEN}
\begin{itemize}
\item
If \emph{y} is a \emph{constituent} or \emph{non-terminating macro},
then do the following.
The case of \emph{y\/} should not be altered; instead,
\emph{y} should be regarded as replaceable.
Append \emph{y} to the token being built,
and repeat step~\ref{READER-PLAIN-TOKEN}.
\item
If \emph{y} is a \emph{single escape} character, then read the next character
and call it \emph{z}
(but if at end of file, signal an error instead).
Ignore the usual syntax of \emph{z}
and pretend it is a \emph{constituent} whose only attribute is
\emph{alphabetic}.
For the purposes of \cdf{readtable-case}, \emph{z} is not replaceable.
Append \emph{z} to the token being built,
and repeat step~\ref{READER-PLAIN-TOKEN}.
\item
If \emph{y} is a \emph{multiple escape} character,
then go to step~\ref{READER-MULTI-TOKEN}.
\item
If \emph{y} is an \emph{illegal} character, signal an error.
\item
If \emph{y} is a \emph{terminating macro} character, it terminates
the token. First ``unread'' the character \emph{y}
(see \cdf{unread-char}), then go to step~\ref{READER-TOKEN-END}.
\item
If \emph{y} is a \emph{whitespace} character, it terminates
the token. First ``unread'' \emph{y}
if appropriate (see \cdf{read-preserving-whitespace}),
then go to step~\ref{READER-TOKEN-END}.
\end{itemize}
\item
(At this point a token is being accumulated, and an odd number
of \emph{multiple escape} characters have been encountered.)
If at end of file, signal an error.
Otherwise, read a character (call it \emph{y}), and
perform one of the following actions according to its syntactic type:
\label{READER-MULTI-TOKEN}
\begin{itemize}
\item
If \emph{y} is a \emph{constituent}, \emph{macro}, or \emph{whitespace}
character, then ignore the usual syntax of that character
and pretend it is a \emph{constituent} whose only attribute is
\emph{alphabetic}.
For the purposes of \cdf{readtable-case}, \emph{y} is not replaceable.
Append \emph{y} to the token being built,
and repeat step~\ref{READER-MULTI-TOKEN}.
\item
If \emph{y} is a \emph{single escape} character, then read the next character
and call it \emph{z}
(but if at end of file, signal an error instead).
Ignore the usual syntax of \emph{z}
and pretend it is a \emph{constituent} whose only attribute is
\emph{alphabetic}.
For the purposes of \cdf{readtable-case}, \emph{z} is not replaceable.
Append \emph{z} to the token being built,
and repeat step~\ref{READER-MULTI-TOKEN}.
\item
If \emph{y} is a \emph{multiple escape} character,
then go to step~\ref{READER-PLAIN-TOKEN}.
\item
If \emph{y} is an \emph{illegal} character, signal an error.
\end{itemize}
\item
An entire token has been accumulated.
\begin{newer}
X3J13 voted in June 1989 \issue{READ-CASE-SENSITIVITY} to introduce
\cdf{readtable-case}. If the accumulated token
is to be interpreted as a symbol, any case conversion of replaceable
characters should be performed at this point according to the value
of the \cdf{readtable-case} slot of the current readtable (the value
of \cdf{*readtable*}).
\end{newer}
Interpret the token as representing
a Lisp object and return that object as the result
of the read operation, or signal an error if the token
is not of legal syntax.
\begin{newer}
X3J13 voted in March 1989 \issue{CHARACTER-PROPOSAL}
to specify that implementation-defined
attributes may be removed from the characters of a symbol token
when constructing the print name.
It is implementation-dependent which attributes are removed.
\end{newer}
\label{READER-TOKEN-END}
\end{enumerate}
\endgroup
As a rule, a \emph{single escape} character never stands for itself but always
serves to cause the following character to be treated as a simple alphabetic
character. A \emph{single escape} character can be included in a token only
if preceded by another \emph{single escape} character.
A \emph{multiple escape} character also never stands for itself. The characters
between a pair of \emph{multiple escape} characters are all treated as
simple alphabetic characters, except that \emph{single escape} and
\emph{multiple escape} characters must nevertheless be preceded by
a \emph{single escape} character to be included.
\subsection{Parsing of Numbers and Symbols}
\label{PARSE-TOKENS-SECTION}
When an extended token is read, it is interpreted as a number or symbol.
In general, the token is interpreted as a number if it satisfies
the syntax for numbers specified in table~\ref{NUMBER-SYNTAX-TABLE};
this is discussed in more detail below.
The characters of the extended token may serve various syntactic
functions as shown
in table~\ref{Standard-Readtable-Attributes-Table}, but it must be
remembered that any character included in a token under the control
of an escape character is treated as \emph{alphabetic} rather than
according to the attributes shown in the table.
One consequence of this rule is that a whitespace, macro, or escape
character will always be treated as alphabetic within an extended token
because such a character cannot be included in an extended
token except under the control of an escape character.
To allow for extensions to the syntax of numbers, a
syntax for \emph{potential numbers} is defined in Common Lisp that is
more general than the actual syntax for numbers.
Any token that is not a potential number and does not consist
entirely of dots will always be taken to be a symbol,
now and in the future; programs may rely on this fact.
Any token that is a potential number but does not fit the
actual number syntax defined below is a \emph{reserved token} and
has an implementation-dependent interpretation;
an implementation may signal an error, quietly treat the token
as a symbol, or take some other action. Programmers should avoid
the use of such reserved tokens. (A symbol whose name looks like a reserved
token can always be written using one or more escape characters.)
Just as \emph{bignum} is the standard term used by Lisp implementors for
very large integers, and \emph{flonum} (rhymes with ``low hum'') refers
to a floating-point number, the term \emph{potnum} has been used widely
as an abbreviation for ``potential number.'' ``Potnum'' rhymes with ``hot rum.''
\goodbreak
A token is a potential number if it satisfies the following
requirements:
\begin{table}[t]
\caption{Actual Syntax of Numbers}
\label{NUMBER-SYNTAX-TABLE}
\tabbingsep=0pt
\normalsize
\begin{tabbing}
\emph{number} ::= \emph{integer} {\Mor} \emph{ratio} {\Mor} \emph{floating-point-number} \\
\emph{integer} ::= \Mopt{\emph{sign}} \Mplus{\emph{digit}} \Mopt{\emph{decimal-point}} \\
\emph{ratio} ::= \Mopt{\emph{sign}} \Mplus{\emph{digit}} \cdf{/} \Mplus{\emph{digit}} \\
\emph{floating-point-number} ::=\= \Mopt{\emph{sign}} \Mstar{\emph{digit}} \emph{decimal-point} \Mplus{\emph{digit}} \Mopt{\emph{exponent}} \\
\>{\Mor} \'\Mopt{\emph{sign}} \Mplus{\emph{digit}} \Mopt{\emph{decimal-point}
\Mstar{\emph{digit}}} \emph{exponent} \\ \emph{sign} ::= \cdf{+} {\Mor} \cdf{-} \\
\emph{decimal-point} ::= \cd{.} \\
\emph{digit} ::= \cd{0} {\Mor} \cd{1} {\Mor} \cd{2} {\Mor} \cd{3} {\Mor} \cd{4}
{\Mor} \cd{5} {\Mor} \cd{6} {\Mor} \cd{7} {\Mor} \cd{8} {\Mor} \cd{9} \\
\emph{exponent} ::= \emph{exponent-marker} \Mopt{\emph{sign}} \Mplus{\emph{digit}} \\
\emph{exponent-marker} ::= \cdf{e} {\Mor} \cdf{s} {\Mor} \cdf{f} {\Mor} \cdf{d} {\Mor} \cdf{l}
{\Mor} \cdf{E} {\Mor} \cdf{S} {\Mor} \cdf{F} {\Mor} \cdf{D} {\Mor} \cdf{L}
\end{tabbing}
\end{table}
\begin{itemize}
\item
It consists entirely of digits, signs (\cdf{+} or \cdf{-}),
ratio markers (\cdf{/}), decimal points (\cd{.}), extension characters
(\cd{{\Xcircumflex}} or \cd{{\Xunderscore}}), and number markers. (A number marker is
a letter. Whether a letter may be treated as a number marker depends
on context, but no letter that is adjacent to another letter may ever be
treated as a number marker. Floating-point exponent markers are instances
of number markers.)
\item
It contains at least one digit. (Letters may be considered to be
digits, depending on the value of \cd{*read-base*}, but only
in tokens containing no decimal points.)
\item
It begins with a digit, sign, decimal point, or extension character.
\item
It does not end with a sign.
\end{itemize}
As examples, the following tokens are potential numbers,
but they are \emph{not} actually numbers as defined below, and so are
reserved tokens. (They do indicate some interesting possibilities
for future extensions.)
\begin{table}
\caption{Standard Constituent Character Attributes}
\label{Standard-Readtable-Attributes-Table}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l@{\extracolsep{\fill}}lllll@{}}
\cd{!}&\emph{alphabetic}&$\langle$page$\rangle$&\emph{illegal}&$\langle$backspace$\rangle$&\emph{illegal} \\
\cd{"}&\emph{alphabetic} *&$\langle$return$\rangle$&\emph{illegal} *&$\langle$tab$\rangle$&\emph{illegal} * \\
\cd{\#}&\emph{alphabetic} *&$\langle$space$\rangle$&\emph{illegal} *&$\langle$newline$\rangle$&\emph{illegal} * \\
\cd{\$}&\emph{alphabetic}&$\langle$rubout$\rangle$&\emph{illegal}&$\langle$linefeed$\rangle$&\emph{illegal} * \\
\cd{\%}&\emph{alphabetic}&\cd{.}&\multicolumn{3}{l}{\emph{alphabetic}, \emph{dot}, \emph{decimal point}}\\
\cd{\&}&\emph{alphabetic}&\cdf{+}&\multicolumn{3}{l}{\emph{alphabetic}, \emph{plus sign}} \\
\cd{'}&\emph{alphabetic} *&\cdf{-}&\multicolumn{3}{l}{\emph{alphabetic}, \emph{minus sign}} \\
\cd{(}&\emph{alphabetic} *&\cdf{*}&\emph{alphabetic} \\
\cd{)}&\emph{alphabetic} *&\cdf{/}&\multicolumn{3}{l}{\emph{alphabetic}, \emph{ratio marker}} \\
\cd{,}&\emph{alphabetic} *&\cd{{\Xatsign}}&\emph{alphabetic} \\
\cd{0}&\emph{alphadigit}&\cdf{A}, \cdf{a}&\emph{alphadigit} \\
\cd{1}&\emph{alphadigit}&\cdf{B}, \cdf{b}&\emph{alphadigit} \\
\cd{2}&\emph{alphadigit}&\cdf{C}, \cdf{c}&\emph{alphadigit} \\
\cd{3}&\emph{alphadigit}&\cdf{D}, \cdf{d}&\multicolumn{3}{l}{\emph{alphadigit}, \emph{double-float exponent marker}} \\
\cd{4}&\emph{alphadigit}&\cdf{E}, \cdf{e}&\multicolumn{3}{l}{\emph{alphadigit}, \emph{float exponent marker}} \\
\cd{5}&\emph{alphadigit}&\cdf{F}, \cdf{f}&\multicolumn{3}{l}{\emph{alphadigit}, \emph{single-float exponent marker}} \\
\cd{6}&\emph{alphadigit}&\cdf{G}, \cdf{g}&\emph{alphadigit} \\
\cd{7}&\emph{alphadigit}&\cdf{H}, \cdf{h}&\emph{alphadigit} \\
\cd{8}&\emph{alphadigit}&\cdf{I}, \cdf{i}&\emph{alphadigit} \\
\cd{9}&\emph{alphadigit}&\cdf{J}, \cdf{j}&\emph{alphadigit} \\
\cd{:}&\emph{package marker}~~~~~~&\cdf{K}, \cdf{k}&\emph{alphadigit} \\
\cd{;}&\emph{alphabetic} *&\cdf{L}, \cdf{l}&\multicolumn{3}{l}{\emph{alphadigit}, \emph{long-float exponent marker}} \\
\cdf{<}&\emph{alphabetic}&\cdf{M}, \cdf{m}&\emph{alphadigit} \\
\cdf{=}&\emph{alphabetic}&\cdf{N}, \cdf{n}&\emph{alphadigit} \\
\cdf{>}&\emph{alphabetic}&\cdf{O}, \cdf{o}&\emph{alphadigit} \\
\cd{?}&\emph{alphabetic}&\cdf{P}, \cdf{p}&\emph{alphadigit} \\
\cd{{\Xlbracket}}&\emph{alphabetic}&\cdf{Q}, \cdf{q}&\emph{alphadigit} \\
\cd{{\Xbackslash}}&\emph{alphabetic} *&\cdf{R}, \cdf{r}&\emph{alphadigit} \\
\cd{{\Xrbracket}}&\emph{alphabetic}&\cdf{S}, \cdf{s}&\multicolumn{3}{l}{\emph{alphadigit}, \emph{short-float exponent marker}} \\
\cd{{\Xcircumflex}}&\emph{alphabetic}&\cdf{T}, \cdf{t}&\emph{alphadigit} \\
\cd{{\Xunderscore}}&\emph{alphabetic}&\cdf{U}, \cdf{u}&\emph{alphadigit} \\
\cd{{\Xbq}}&\emph{alphabetic} *&\cdf{V}, \cdf{v}&\emph{alphadigit} \\
\cd{{\Xlbrace}}&\emph{alphabetic}&\cdf{W}, \cdf{w}&\emph{alphadigit} \\
\cd{|}&\emph{alphabetic} *&\cdf{X}, \cdf{x}&\emph{alphadigit} \\
\cd{{\Xrbrace}}&\emph{alphabetic}&\cdf{Y}, \cdf{y}&\emph{alphadigit} \\
\cd{{\Xtilde}}&\emph{alphabetic}&\cdf{Z}, \cdf{z}&\emph{alphadigit} \\
\end{tabular*}
\vfill
\begin{footnotesize}
\noindent
These interpretations apply only to characters whose
syntactic type is \emph{constituent}. Entries marked
with an asterisk are normally shadowed because the characters
are of syntactic type
\emph{whitespace}, \emph{macro}, \emph{single escape}, or \emph{multiple escape}.
An \emph{alphadigit} character is interpreted as a
digit if it is a valid digit in the radix specified by {\small \cd{*read-base*}};
otherwise it is alphabetic.
Characters with an \emph{illegal} attribute can never appear in
a token except under the control of an escape character.
\end{footnotesize}
\end{table}
\begin{lisp}
\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\kill
1b5000\>777777q\>1.7J\>-3/4+6.7J\>12/25/83 \\
27{\Xcircumflex}19\>3{\Xcircumflex}4/5\>6//7\>3.1.2.6\>{\Xcircumflex}-43{\Xcircumflex} \\
3.141{\Xunderscore}592{\Xunderscore}653{\Xunderscore}589{\Xunderscore}793{\Xunderscore}238{\Xunderscore}4\>\>\>-3.7+2.6i-6.17j+19.6k
\end{lisp}
The following tokens are \emph{not} potential numbers but are always
treated as symbols:
\begin{lisp}
\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\kill
/\>/5\>+\>1+\>1- \\*
foo+\>ab.cd\>{\Xunderscore}\>{\Xcircumflex}\>{\Xcircumflex}/-
\end{lisp}
The following tokens are potential numbers if the value of
\cd{*read-base*} is \cd{16} (an abnormal situation), but they are
always treated as symbols if the value of \cd{*read-base*}
is \cd{10} (the usual value):
\begin{lisp}
\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\hskip 0.2\linewidth\=\kill
bad-face\>25-dec-83\>a/b\>fad{\Xunderscore}cafe\>f{\Xcircumflex}
\end{lisp}
It is possible for there to be an ambiguity as to whether
a letter should be treated as a digit or as a number marker.
In such a case, the letter is always treated as a digit
rather than as a number marker.
Note that the printed representation for a potential
number may not contain any escape characters.
An escape character robs the following character of all syntactic
qualities, forcing it to be strictly alphabetic and therefore unsuitable
for use in a potential number. For example,
all of the following representations are interpreted as symbols, not numbers:
\begin{lisp}
{\Xbackslash}256~~~25{\Xbackslash}64~~~1.0{\Xbackslash}E6~~~|100|~~~3{\Xbackslash}.14159~~~|3/4|~~~3{\Xbackslash}/4~~~5||
\end{lisp}
In each case, removing the escape character(s) would allow the token
to be treated as a number.
If a potential number can in fact
be interpreted as a number according to the BNF
syntax in table~\ref{NUMBER-SYNTAX-TABLE}, then a number object of the
appropriate type is constructed and returned. It should be noted that in
a given implementation it may be that not all tokens conforming to the
actual syntax for numbers can actually be converted into number objects.
For example, specifying too large or too small an exponent for a floating-point
number may make the number impossible to represent in the implementation.
Similarly, a ratio with denominator zero (such as \cd{-35/000})
cannot be represented in \emph{any} implementation.
In any such circumstance where
a token with the syntax of a number cannot be converted to an internal
number object, an error is signaled. (On the other hand, an error
must not be signaled for specifying too many significant digits
for a floating-point number; an appropriately truncated or rounded
value should be produced.)
There is an omission in the syntax of numbers
as described in table~\ref{NUMBER-SYNTAX-TABLE},
in that the syntax does not account for the possible
use of letters as digits.
The radix used for reading integers and ratios is normally decimal.
However, this radix is actually determined by the value of
the variable \cd{*read-base*}, whose initial value is \cd{10}.
\cd{*read-base*} may take on any integral value between \cd{2} and \cd{36};
let this value be \emph{n}. Then a token \emph{x} is interpreted as
an integer or ratio in base \emph{n} if it could be properly
so interpreted in the syntax \cd{\#\emph{n}R\emph{x}}
(see section~\ref{SHARP-SIGN-MACRO-CHARACTER-SECTION}).
So, for example, if the value of \cd{*read-base*} is \cd{16},
then the printed representation
\begin{lisp}
(a small face in a bad place)
\end{lisp}
would be interpreted as if the following representation had
been read with \cd{*read-base*} set to \cd{10}:
\begin{lisp}
(10 small 64206 in 10 2989 place)
\end{lisp}
because four of the seven tokens in the list can be interpreted
as hexadecimal numbers. This facility is intended to be used
in reading files of data that for some reason contain numbers
not in decimal radix; it may also be used for reading programs
written in Lisp dialects (such as MacLisp) whose default number radix is not
decimal. Non-decimal constants in Common Lisp programs
or portable Common Lisp data files should be written using
\cd{\#O}, \cd{\#X}, \cd{\#B}, or \cd{\#\emph{n}R} syntax.
When \cd{*read-base*} has a value greater than \cd{10}, an ambiguity
is introduced into the actual syntax for numbers because a letter can serve
as either a digit or an exponent marker; a simple example is \cd{1E0}
when the value of \cd{*read-base*} is \cd{16}. The ambiguity is resolved
in accordance with the general principle that interpretation
as a digit is preferred to interpretation as a number marker.
The consequence in this case is that
if a token can be interpreted as either an integer or a floating-point
number, then it is taken to be an integer.
If a token consists solely of dots (with no escape characters), then an
error is signaled, except in one circumstance: if the token is a single
dot and occurs in a situation appropriate to ``dotted list'' syntax,
then it is accepted as a part of such syntax. Signaling an error
catches not only misplaced dots in dotted list syntax but also
lists that were truncated by \cd{*print-length*} cutoff,
because such lists end with a three-dot sequence (\cd{...}).
Examples:
\begin{lisp}
~~~~~~~~~~~~~~~~\=\kill
(a . b)\>;\textrm{A dotted pair of \cdf{a} and \cdf{b}} \\
(a.b)\>;\textrm{A list of one element, the symbol named \cd{a.b}} \\
(a. b)\>;\textrm{A list of two elements \cd{a.} and \cdf{b}} \\
(a .b)\>;\textrm{A list of two elements \cdf{a} and \cd{.b}} \\
(a {\Xbackslash}. b)\>;\textrm{A list of three elements \cdf{a}, \cd{.}, and \cdf{b}} \\
(a |.| b)\>;\textrm{A list of three elements \cdf{a}, \cd{.}, and \cdf{b}} \\
(a {\Xbackslash}... b)\>;\textrm{A list of three elements \cdf{a}, \cd{...}, and \cdf{b}} \\
(a |...| b)\>;\textrm{A list of three elements \cdf{a}, \cd{...}, and \cdf{b}} \\
(a b . c)\>;\textrm{A dotted list of \cdf{a} and \cdf{b} with \cdf{c} at the end} \\
.iot\>;\textrm{The symbol whose name is \cd{.iot}} \\
(. b)\>;\textrm{Illegal; an error is signaled} \\
(a .)\>;\textrm{Illegal; an error is signaled} \\
(a .. b)\>;\textrm{Illegal; an error is signaled} \\
(a . . b)\>;\textrm{Illegal; an error is signaled} \\
(a b c ...)\>;\textrm{Illegal; an error is signaled}
\end{lisp}
In all other cases, the token is construed to be the name of a symbol.
If there are any package markers
(colons) in the token, they divide the token into pieces used to
control the lookup and creation of the symbol.
\begin{obsolete}
If there is a single package marker, and it occurs at the beginning of the
token, then the token is interpreted as a keyword, that is, a symbol in the
\cdf{keyword} package. The part of the token after the package marker must
not have the syntax of a number.
If there is a single package marker not at the beginning or end of the
token, then it divides the token into two parts. The first part
specifies a package; the second part is the name of an external symbol
available in that package. Neither of the two parts may have the syntax
of a number.
If there are two adjacent package markers not at the beginning or end of the
token, then they divide the token into two parts. The first part
specifies a package; the second part is the name of a symbol within
that package (possibly an internal symbol).
Neither of the two parts may have the syntax of a number.
\end{obsolete}
\begin{new}
X3J13 voted in March 1988
\issue{COLON-NUMBER}
to clarify that, in the situations described in the
preceding three paragraphs, the restriction on the syntax of the parts should
be strengthened: none of the parts may have the syntax of even
a \emph{potential} number. Tokens such as \cd{:3600}, \cd{:1/2},
and \cd{editor:3.14159} were already ruled out; this clarification further
declares that such tokens as \cd{:2{\Xcircumflex} 3}, \cd{compiler:1.7J},
and \cd{Christmas:12/25/83} are also in error and therefore should not be used
in portable programs. Implementations may differ in their treatment of
such package-marked potential numbers.
\end{new}
If a symbol token contains no package markers, then the entire token
is the name of the symbol. The symbol is looked up in
the default package, which is the value
of the variable \cdf{*package*}.
All other patterns of package markers,
including the cases where there are more than two
package markers or where a package marker appears at the end of the token,
at present do not mean anything in Common Lisp (see chapter~\ref{XPACK}).
It is therefore currently an error to use such patterns in a Common Lisp program.
The valid patterns for tokens may be summarized as follows:
\begin{tabbing}
\hskip 8pc\=\kill
\emph{nnnnn}\>a number \\
\emph{xxxxx}\>a symbol in the current package \\
\cd{:\emph{xxxxx}}\>a symbol in the keyword package \\
\cd{\emph{ppppp}:\emph{xxxxx}}\>an external symbol in the \emph{ppppp} package \\
\cd{\emph{ppppp}::\emph{xxxxx}}\>a (possibly internal) symbol in the \emph{ppppp} package
\end{tabbing}
where \emph{nnnnn} has the syntax of a number, and \emph{xxxxx} and \emph{ppppp} do
not have the syntax of a number.
\begin{new}
In accordance with the X3J13 decision noted above
\issue{COLON-NUMBER}, \emph{xxxxx} and \emph{ppppp} may not have the syntax of even
a potential number.
\end{new}
\begin{defun}[Variable]
*read-base*
The value of \cd{*read-base*} controls the interpretation of tokens
by \cdf{read} as being integers or ratios. Its value is the radix
in which integers and ratios are to be read; the value may be any integer
from \cd{2} to \cd{36} (inclusive) and is normally \cd{10} (decimal radix).
Its value affects only the reading of integers and ratios.
In particular, floating-point numbers are always read in decimal radix.
The value of \cd{*read-base*} does not affect the radix for rational numbers
whose radix is explicitly indicated by
\cd{\#O}, \cd{\#X}, \cd{\#B}, or \cd{\#\emph{n}R} syntax
or by a trailing decimal point.
Care should be taken when setting \cd{*read-base*} to a value larger
than \cd{10}, because tokens that would normally be interpreted as
symbols may be interpreted as numbers instead. For example,
with \cd{*read-base*} set to \cd{16} (hexadecimal radix), variables
with names such as \cdf{a}, \cdf{b}, \cdf{f}, \cdf{bad}, and \cdf{face}
will be treated by the reader as numbers (with decimal values
10, 11, 15, 2989, and 64206, respectively). The ability to alter
the input radix is provided in Common Lisp primarily for the purpose of
reading data files in special operatorats, rather than for the purpose of altering
the default radix in which to read programs. The user is strongly
encouraged to use \cd{\#O}, \cd{\#X}, \cd{\#B}, or \cd{\#\emph{n}R} syntax when
notating non-decimal constants in programs.
\end{defun}
\begin{defun}[Variable]
*read-suppress*
When the value of \cd{*read-suppress*} is {\nil}, the Lisp reader
operates normally. When it is not {\nil}, then most of the interesting
operations of the reader are suppressed; input characters are parsed,
but much of what is read is not interpreted.
The primary purpose of \cd{*read-suppress*} is to support the operation of
the read-time conditional constructs \cd{\#+} and \cd{\#-}
(see section~\ref{SHARP-SIGN-MACRO-CHARACTER-SECTION}). It is important for these
constructs to be able to skip over the printed representation of a Lisp
expression despite the possibility that the syntax of the skipped
expression may not be entirely legal for the current implementation; this
is because a primary application of \cd{\#+} and \cd{\#-} is to allow the
same program to be shared among several Lisp implementations despite
small incompatibilities of syntax.
A non-{\nil} value of \cd{*read-suppress*} has the following specific
effects on the Common Lisp reader:
\begin{itemize}
\item
All extended tokens are completely uninterpreted. It matters not
whether the token looks like a number, much less like a valid number;
the pattern of package markers also does not matter. An extended token
is simply discarded and treated as if it were {\nil}; that is, reading
an extended token when \cd{*read-suppress*} is non-{\nil} simply returns {\nil}.
(One consequence of this is that the error concerning improper
dotted-list syntax will not be signaled.)
\item
Any standard
\cd{\#} macro-character construction that requires, permits, or disallows
an infix numerical argument, such as \cd{\#\emph{n}R}, will not enforce
any constraint on the presence, absence, or value of such an argument.
\item
The \cd{\#{\Xbackslash}} construction always produces the value {\nil}.
It will not signal an error even if an unknown character name is seen.
\item
Each of the \cd{\#B}, \cd{\#O}, \cd{\#X}, and \cd{\#R}
constructions always scans over a following token and produces the value {\nil}.
It will not signal an error even if the token does not have the syntax
of a rational number.
\item
The \cd{\#*} construction always scans over a following token and
produces the value {\nil}.
It will not signal an error even if the token does not consist solely
of the characters \cd{0} and \cd{1}.
\end{itemize}
\begin{itemize}
\item
The \cd{\#.} construction reads the following
form (in suppressed mode, of course) but does not evaluate it.
The form is discarded and {\nil} is produced.
\end{itemize}
\begin{itemize}
\item
Each of the \cd{\#A}, \cd{\#S}, and \cd{\#:}
constructions reads the following
form (in suppressed mode, of course) but does not interpret it in any way;
it need not even be a list in the case of \cd{\#S}, or a symbol
in the case of \cd{\#:}. The form is discarded and {\nil} is produced.
\item
The \cd{\#=} construction is totally ignored. It does not read
a following form. It produces no object, but is treated as whitespace.
\item
The \cd{\#\#} construction always produces {\nil}.
\end{itemize}
Note that, no matter what the value of \cd{*read-suppress*},
parentheses still continue to delimit (and construct) lists;
the \cd{\#(} construction continues to delimit vectors;
and comments, strings, and the quote and backquote constructions continue to be
interpreted properly. Furthermore, such situations as
\cd{')},
\cd{\#<}, \cd{\#)}, and \cd{\#$\langle$\textrm{space}$\rangle$} continue to signal errors.
In some cases, it may be appropriate for a user-written macro-character
definition to check the value of \cd{*read-suppress*} and to avoid certain
computations or side effects if its value is not {\nil}.
\end{defun}
\begin{defun}[Variable]
*read-eval*
Default value of \cd{*read-eval*} is \cdf{t}.
If \cd{*read-eval*} is false, the \cd{\#.} reader macro signals an error.
Printing is also affected. If
\cd{*read-eval*} is false and \cd{*print-readably*} is true, any \cdf{print-object}
method that would otherwise output a \cd{\#.} reader macro must either output something
different or signal an error of type \cdf{print-not-readable}.
Binding \cd{*read-eval*} to \cdf{nil} is useful when reading data that came from
an untrusted source, such as a network or a user-supplied data file; it
prevents the \cd{\#.} reader macro from being exploited as a ``Trojan horse'' to
cause arbitrary forms to be evaluated.
\end{defun}
\subsection{Macro Characters}
\label{MACRO-CHARACTERS-SECTION}
\indexterm{parsing}
\indexterm{macro character}
If the reader encounters a macro
character, then the function associated with that macro character is
invoked and may produce an object to be returned. This function may read
following characters in the stream in whatever syntax it likes (it may
even call \cdf{read} recursively) and return the object represented by
that syntax. Macro characters may or may not be recognized, of course,
when read as part of other special syntaxes (such as for strings).
The reader is therefore organized into two parts: the basic dispatch loop,
which also distinguishes symbols and numbers, and the collection
of macro characters. Any character can be reprogrammed as a macro character;
this is a means by which the reader can be extended.
The macro characters normally defined are as follows:
\begin{flushdesc}
\item[\cd{(}]
\indexterm{(!macro character}
The left-parenthesis character initiates reading of a pair or list.
The function~\cdf{read} is called recursively to read successive objects
until a right parenthesis is found to be next in the input stream.
A list of the objects read is returned. Thus the input sequence
\begin{lisp}
(a b c)
\end{lisp}
is read as a list of three objects (the symbols \cdf{a}, \cdf{b}, and \cdf{c}).
The right parenthesis need not immediately follow the printed representation of
the last object; whitespace
characters and comments may precede it.
This can be useful for putting one object
on each line and making it easy to add new objects:
\begin{lisp}
(defun traffic-light (color) \\
~~(case color \\
~~~~(green) \\
~~~~(red (stop)) \\
~~~~(amber (accelerate))~~~~~;\textrm{Insert more colors after this line} \\
~~~~))
\end{lisp}
It may be that \emph{no} objects precede the right parenthesis, as in \cd{()}
or \cd{(~)}; this reads as a list of zero objects (the empty list).
If a token that is just a dot,
not preceded by an escape character,
is read after some object,
then exactly one more object must follow the dot,
possibly followed by whitespace,
followed by the right parenthesis:
\begin{lisp}
(a b c . d)
\end{lisp}
This means that the \emph{cdr} of the last pair in the list is not {\nil},
but rather the object whose representation followed the dot.
The above example might have been the result of evaluating
\begin{lisp}
(cons 'a (cons 'b (cons 'c 'd))) \EV\ (a b c . d)
\end{lisp}
Similarly, we have
\begin{lisp}
(cons 'znets 'wolq-zorbitan) \EV\ (znets . wolq-zorbitan)
\end{lisp}
It is permissible for the object following the dot to be a list:
\begin{lisp}
(a b c d . (e f . (g)))
\end{lisp}
is the same as
\begin{lisp}
(a b c d e f g)
\end{lisp}
but a list following a dot
is a non-standard form that \cdf{print} will never produce.
\item[\cd{)}]
\indexterm{)!macro character}
The right-parenthesis character is part of various constructs
(such as the syntax for lists) using the left-parenthesis character and
is invalid except when used in such a construct.
%\indexterm{\cd{) }macro character} fix label
%\indexterm{)}
\newpage%required
\item[\cd{{\Xquote}}]
\indexterm{'!macro character}
The single-quote (accent acute) character provides
an abbreviation to make it easier to put constants in programs.
The form
\cd{'\emph{foo}} reads the same as \cd{(quote \emph{foo})}: a list of the symbol
\cdf{quote} and \emph{foo}.
\item[\cd{;}]
Semicolon is used to write comments.
\indexterm{;!macro character}
\indexterm{comments}
The semicolon and all characters
up to and including the next newline are ignored.
Thus a comment can be put at
the end of any line without affecting the reader.
(A comment will terminate a token, but a newline would terminate the
token anyway.)
There is no functional difference between using one semicolon and using
more than one, but the conventions shown here are in common use.
\begin{lisp}
;;;; COMMENT-EXAMPLE function. \\
;;; This function is useless except to demonstrate comments. \\
;;; (Actually, this example is much too cluttered with them.) \\
\\
(defun comment-example (x y)~~~~~~;X is anything; Y is an a-list. \\
~~(cond ((listp x) x)~~~~~~~~~~~~~;If X is a list, use that. \\
~~~~~~~~;; X is now not a list. There are two other cases. \\
~~~~~~~~((symbolp x) \\
~~~~~~~~~;; Look up a symbol in the a-list. \\
~~~~~~~~~(cdr (assoc x y)))~~~~~~~;Remember, (cdr {\false}) is {\false}. \\
~~~~~~~~;; Do this when all else fails: \\
~~~~~~~~(t (cons x~~~~~~~~~~~~~~~~;Add x to a default list. \\
~~~~~~~~~~~~~~~~~'((lisp t)~~~~~~~;LISP is okay. \\
~~~~~~~~~~~~~~~~~~~(fortran nil)~~;FORTRAN is not. \\
~~~~~~~~~~~~~~~~~~~(pl/i -500)~~~~;Note that you can put comments in \\
~~~~~~~~~~~~~~~~~~~(ada .001)~~~~~; "data" as well as in "programs". \\
~~~~~~~~~~~~~~~~~~~;; COBOL?? \\
~~~~~~~~~~~~~~~~~~~(teco -1.0e9))))))
\end{lisp}
In this example, comments may begin with one to four semicolons.