Common Lisp the Language, 2nd Edition
The standard syntax includes forms introduced by the # character. These take the general form of a #, a second character that identifies the syntax, and following arguments in some form. If the second character is a letter, then case is not important; #O and #o are considered to be equivalent, for example.
Certain # forms allow an unsigned decimal number to appear between the # and the second character; some other forms even require it. Those forms that do not explicitly permit such a number to appear forbid it.
---------------------------------------------------------------- Table 22-4: Standard # Macro Character Syntax #! undefined * #<backspace> signals error #" undefined #<tab> signals error ## reference to #= label #<newline> signals error #$ undefined #<linefeed> signals error #% undefined #<page> signals error #& undefined #<return> signals error #' function abbreviation #<space> signals error #( simple vector #+ read-time conditional #) signals error #- read-time conditional #* bit-vector #. read-time evaluation #, load-time evaluation #/ undefined #0 used for infix arguments #A, #a array #1 used for infix arguments #B, #b binary rational #2 used for infix arguments #C, #c complex number #3 used for infix arguments #D, #d undefined #4 used for infix arguments #E, #e undefined #5 used for infix arguments #F, #f undefined #6 used for infix arguments #G, #g undefined #7 used for infix arguments #H, #h undefined #8 used for infix arguments #I, #i undefined #9 used for infix arguments #J, #j undefined #: uninterned symbol #K, #k undefined #; undefined #L, #l undefined #< signals error #M, #m undefined #= label following object #N, #n undefined #> undefined #O, #o octal rational #? undefined * #P, #p pathname #@ undefined #Q, #q undefined #[ undefined * #R, #r radix-n rational #\ character object #S, #s structure #] undefined * #T, #t undefined #^ undefined #U, #u undefined #_ undefined #V, #v undefined #` undefined #W, #w undefined #{ undefined * #X, #x hexadecimal rational #| balanced comment #Y, #y undefined #} undefined * #Z, #z undefined #~ undefined #<rubout> undefined The combinations marked by an asterisk are explicitly reserved to the user and will never be defined by Common Lisp.
X3J13 voted in June 1989 (PATHNAME-PRINT-READ) to specify #P and #p (undefined in the first edition).
----------------------------------------------------------------
The currently defined # constructs are described below and summarized in table 22-4; more are likely to be added in the future. However, the constructs #!, #?, #[, #], #{, and #} are explicitly reserved for the user and will never be defined by the Common Lisp standard.
In the single-character case, the character x must be followed by a non-constituent character, lest a name appear to follow the #\. A good model of what happens is that after #\ is read, the reader backs up over the and then reads an extended token, treating the initial as an escape character (whether it really is or not in the current readtable).
Uppercase and lowercase letters are distinguished after #\; #\A and #\a denote different character objects. Any character works after #\, even those that are normally special to read, such as parentheses. Non-printing characters may be used after #\, although for them names are generally preferred.
#\name reads in as a character object whose name is name (actually, whose name is (string-upcase name); therefore the syntax is case-insensitive). The name should have the syntax of a symbol. The following names are standard across all implementations:
newline The character that represents the division between lines space The space or blank characterThe following names are semi-standard; if an implementation supports them, they should be used for the described characters and no others.
rubout The rubout or delete character. page The form-feed or page-separator character tab The tabulate character backspace The backspace character return The carriage return character linefeed The line-feed characterIn some implementations, one or more of these characters might be a synonym for a standard character; the #\Linefeed character might be the same as #\Newline, for example.
When the Lisp printer types out the name of a special character, it uses the same table as the #\ reader; therefore any character name you see typed out is acceptable as input (in that implementation). Standard names are always preferred over non-standard names for printing.
The following convention is used in implementations that support non-zero bits attributes for character objects. If a name after #\ is longer than one character and has a hyphen in it, then it may be split into the two parts preceding and following the first hyphen; the first part (actually, string-upcase of the first part) may then be interpreted as the name or initial of a bit, and the second part as the name of the character (which may in turn contain a hyphen and be subject to further splitting). For example:
#\Control-Space #\Control-Meta-Tab #\C-M-Return #\H-S-M-C-Rubout
If the character name consists of a single character, then that character is used. Another may be necessary to quote the character.
#\Control-% #\Control-Meta-\" #\Control-\a #\Meta->
If an unsigned decimal integer appears between the # and (, it specifies explicitly the length of the vector. In that case, it is an error if too many objects are specified before the closing ), and if too few are specified, the last object (it is an error if there are none in this case) is used to fill all remaining elements of the vector. For example,
#(a b c c c c) #6(a b c c c c) #6(a b c) #6(a b c c)
all mean the same thing: a vector of length 6 with elements a, b, and four instances of c. The notation #() denotes an empty vector, as does #0() (which is legitimate because it is not the case that too few elements are specified).
If an unsigned decimal integer appears between the # and *, it specifies explicitly the length of the vector. In that case, it is an error if too many bits are specified, and if too few are specified the last one (it is an error if there are none in this case) is used to fill all remaining elements of the bit-vector. For example,
#*101111 #6*101111 #6*101 #6*1011
all mean the same thing: a vector of length 6 with elements 1, 0, 1, 1, 1, and 1. The notation #* denotes an empty bit-vector, as does #0* (which is legitimate because it is not the case that too few elements are specified).
Both #. and #, allow you to include, in an expression being read, an object that does not have a convenient printed representation; instead of writing a representation for the object, you write an expression that will compute the object.
X3J13 voted in January 1989
(SHARP-COMMA-CONFUSION)
to remove #, from the language.
X3J13 noted that the first edition failed to make it clear that #,
can be meaningful only within quoted forms. All sorts of anomalies can arise,
including inconsistencies between the interpreter and compiler, if #,
is not properly restricted. See load-time-eval.
For example, #3r102 is another way of writing 11, and #11R32 is another way of writing 35. For radices larger than 10, letters of the alphabet are used in order for the digits after 9.
The value of n makes a difference: #2A((0 1 5) (foo 2 (hot dog))), for example, represents a 2-by-3 matrix:
0 1 5 foo 2 (hot dog)
In contrast, #1A((0 1 5) (foo 2 (hot dog))) represents a length-2 array whose elements are lists:
(0 1 5) (foo 2 (hot dog))
Furthermore, #0A((0 1 5) (foo 2 (hot dog))) represents a zero-dimensional array whose sole element is a list:
((0 1 5) (foo 2 (hot dog)))
Similarly, #0Afoo (or, more readably, #0A foo) represents a zero-dimensional array whose sole element is the symbol foo. The expression #1Afoo would not be legal because foo is not a sequence.
#.(cm keyword1 'value1 keyword2 'value2 ...)
where each keywordj is the result of computing
(intern (string slotj) 'keyword)
(This computation is made so that one need not write a colon in front of every slot name.) The net effect is that the constructor macro is called with the specified slots having the specified values (note that one does not write quote marks in the #S syntax). Whatever object the constructor macro returns is returned by the #S syntax.
(setq x (list 'p 'q)) (setq y (list (list 'a 'b) x 'foo x)) (rplacd (last y) (cdr y))
could be represented in this way:
((a b) . #1=(#2=(p q) foo #2# . #1#))
Without this notation, but with *print-length* set to 10, the structure would print in this way:
((a b) (p q) foo (p q) (p q) foo (p q) (p q) foo (p q) ...)
A reference #n# may occur only after a label #n=; forward references are not permitted. In addition, the reference may not appear as the labelled object itself (that is, one may not write #n= #n#), because the object labelled by #n= is not well defined in this case.
#+feature form
If feature is ``true,'' then this syntax represents a Lisp object whose printed representation is form. If feature is ``false,'' then this syntax is effectively whitespace; it is as if it did not appear.
The feature should be the printed representation of a symbol or list. If feature is a symbol, then it is true if and only if it is a member of the list that is the value of the global variable *features*.
For example, suppose that in implementation A the features spice and perq are true, and in implementation B the feature lispm is true. Then the expressions on the left below are read the same as those on the right in implementation A:
The #+ construction must be used judiciously if unreadable code is not to result. The user should make a careful choice between read-time conditionalization and run-time conditionalization.
X3J13 voted in March 1988 (SHARPSIGN-PLUS-MINUS-PACKAGE) to specify that the keyword package is the default package during the reading of a feature specification. Thus #+spice means the same thing as #+:spice, and #+(or spice lispm) means the same thing as #+(or :spice :lispm). Symbols in other packages may be used as feature names, but one must use an explicit package prefix to cite one after #+.
The main purpose of this construct is to allow ``commenting out'' of blocks of code or data. The balancing rule allows such blocks to contain pieces already so commented out. In this respect the #|...|# syntax of Common Lisp differs from the /*...*/ comment syntax used by PL/I and C.
X3J13 voted in June 1989 (DATA-IO) to add print-unreadable-object, a macro that prints an object using #<...> syntax and also takes care of checking the variable *print-readably*.