elisp: Streams Intro
18.1 Introduction to Reading and Printing
=========================================
“Reading” a Lisp object means parsing a Lisp expression in textual form
and producing a corresponding Lisp object. This is how Lisp programs
get into Lisp from files of Lisp code. We call the text the “read
syntax” of the object. For example, the text ‘(a . 5)’ is the read
syntax for a cons cell whose CAR is ‘a’ and whose CDR is the number 5.
“Printing” a Lisp object means producing text that represents that
object—converting the object to its “printed representation” (
Printed Representation). Printing the cons cell described above
produces the text ‘(a . 5)’.
Reading and printing are more or less inverse operations: printing
the object that results from reading a given piece of text often
produces the same text, and reading the text that results from printing
an object usually produces a similar-looking object. For example,
printing the symbol ‘foo’ produces the text ‘foo’, and reading that text
returns the symbol ‘foo’. Printing a list whose elements are ‘a’ and
‘b’ produces the text ‘(a b)’, and reading that text produces a list
(but not the same list) with elements ‘a’ and ‘b’.
However, these two operations are not precisely inverse to each
other. There are three kinds of exceptions:
• Printing can produce text that cannot be read. For example,
buffers, windows, frames, subprocesses and markers print as text
that starts with ‘#’; if you try to read this text, you get an
error. There is no way to read those data types.
• One object can have multiple textual representations. For example,
‘1’ and ‘01’ represent the same integer, and ‘(a b)’ and ‘(a .
(b))’ represent the same list. Reading will accept any of the
alternatives, but printing must choose one of them.
• Comments can appear at certain points in the middle of an object’s
read sequence without affecting the result of reading it.