12.7 The Printer
The default printer generally prints core datatypes in such a way that using read on the output produces a value that is equal? to the printed value – when the printed is used in write. When the printer is used in display mode, the printing of strings, byte strings, characters, and symbols changes to render the character/byte content directly to the output port.
When the print-graph parameter is set to #t, then the printer first scans an object to detect cycles. The scan traverses the components of pairs, mutable pairs, vectors, boxes (when print-box is #t), hash tables (when print-hash-table is #t), and fields of structures exposed by struct->vector (when print-struct is #t). If print-graph is #t, then this information is used to display sharing by printing graph definitions and references (see Reading Graph Structure). If a cycle is detected in the initial scan, then print-graph is effectively set to #t automatically.
With the exception of displaying byte strings, printing is defined in terms of Unicode characters; see Ports for information on how a character stream is written to an port’s underlying byte stream.
12.7.1 Printing Symbols
Symbols containing spaces or special characters write using escaping \ and quoting |s. When the read-case-sensitive parameter is set to #f, then symbols containing uppercase characters also use escaping escaping \ and quoting |s. In addition, symbols are quoted with |s or leading \ when they would otherwise print the same as a numerical constant or as a delimited "." (when read-accept-dot is #t).
When read-accept-bar-quote is #t, |s are used in printing when one | at the beginning and one | at the end suffices to correctly print the symbol. Otherwise, \s are always used to escape special characters, instead of quoting them with |s.
When read-accept-bar-quote is #f, then | is not treated as a special character. The following are always special characters:
( ) [ ] [ ] " , ' ` ; \
In addition, # is a special character when it appears at the beginning of the symbol, and when it is not followed by %.
Symbols display without escaping or quoting special characters. That is, the display form of a symbol is the same as the display form of symbol->string applied to the symbol.
12.7.2 Printing Numbers
A number prints the same way in write and display modes.
A complex number that is not a real number always prints as 〈m〉+〈n〉i, where 〈m〉 and 〈n〉 are the printed forms of its real and imaginary parts, respectively.
An inexact real number prints with either a . decimal point, an e exponent marker, or both. The form is selected so that the output is as short as possible, with the constraint that reading the printed form back in produces an "equal?" number.
An exact 0 prints as 0.
A positive, exact integer prints as a sequence of decimal digits that does not start with 0.
A positive, exact, real, non-integer number prints as 〈m〉/〈n〉, where 〈m〉 and 〈n〉 are the printed forms of the number’s numerators and denominator (as determined by numerator and denominator).
A negative exact number prints with a - prefix on the printed form of its exact negation.
12.7.3 Printing Booleans
The constant #t prints as #t, and the constant #f prints as #f in both display and write modes.
12.7.4 Printing Pairs and Lists
A pair prints starting with ( followed by the printed form of its car. The rest of the printed form depends on the cdr:
If the cdr is a pair or the empty list, then the printed form of the pair completes with the printed form of the cdr, except that the leading ( in the cdr’s printed form is omitted.
Otherwise, the printed for of the pair continues with a space, ., another space, the printed form of the cdr, and a ).
The printed form of a pair is the same in both write and display modes, except as the printed form of the pair’s carand cdr vary with the mode.
By default, mutable pairs (as created with mcons) print the same as pairs, except that { and } are used instead of ( and ). Note that the reader treats {...} and (...) equivalently on input, creating immutable pairs in both cases.
If the print-pair-curly-braces parameter is set to #t, then immutable pairs print using { and }. If the print-mpair-curly-braces parameter is set to #f, then mutable pairs print using ( and ).
12.7.5 Printing Strings
All strings display as their literal character sequences.
The write form of a string starts with " and ends with another ". Between the "s, each character is represented. Each graphic or blank character is represented as itself, with two exceptions: " is printed as \", and \ is printed as \\. Each non-graphic, non-blank character (according to char-graphic? and char-blank?) is printed using the escape sequences described in Reading Strings, using \a, \b, \t, \n, \v, \f, \r, or \e if possible, otherwise using \u with four hexadecimal digits or \U with eight hexadecimal digits (using the latter only if the character value does not fit into four digits).
All byte strings display as their literal byte sequence; this byte sequence may not be a valid UTF-8 encoding, so it may not correspond to a sequence of characters.
The write form a byte string starts with #" and ends with another ". Between the "s, each byte is written using the corresponding ASCII decoding if the byte is between 0 and 127 and the character is graphic or blank (according to char-graphic? and char-blank?). Otherwise, the byte is written using \a, \b, \t, \n, \v, \f, \r, or \e if possible, otherwise using \ followed by one to three octal digits (only as many as necessary).
12.7.6 Printing Vectors
In display mode, the printed form of a vector is # followed by the printed form of vector->list applied to the vector. In write mode, the printed form is the same, except that when the print-vector-length parameter is #t, a decimal integer is printed after the #, and a repeated last element is printed only once..
12.7.7 Printing Structures
When the print-struct parameter is set to #t, then the way that structures print depends on details of the structure type for which the structure is an instance:
If the structure type is a prefab structure type, then it prints using #s( followed by the prefab structure type key, then the printed form each field in the structure, and then ).
If the structure has a prop:custom-write property value, then the associated procedure is used to print the structure.
If the structure type is transparent, or if any ancestor is transparent, then the structure prints as the vector produced by struct->vector.
For any other structure type, the structure prints as an unreadable value; see Printing Unreadable Values for more information.
If the print-struct parameter is set to #f, then all structures without a prop:custom-write property print as unreadable values.
12.7.8 Printing Hash Tables
When the print-hash-table parameter is set to #t, a hash table prints starting with #hash( or #hasheq( for a table using equal? or eq? key comparisons, respectively. After this prefix, each key–value mapping is shown as (, the printed form of a key, a space, ., a space, the printed form the corresponding value, and ), with an addition space if the key–value pairs is not the last to be printed. After all key-value pairs, the printed form completes with ).
When the print-hash-table parameter is set to #f, a hash table prints (unreadably) as #<hash>.
12.7.9 Printing Boxes
When the print-box parameter is set to #t, a box prints as #& followed by the printed form of its content.
When the print-box parameter is set to #f, a box prints (unreadably) as #<box>.
12.7.10 Printing Characters
Characters with the special names described in Reading Characters write using the same name. (Some characters have multiple names; the #\newline and #\nul names are used instead of #\newline and #\nul). Other graphic characters (according to char-graphic?) write as #\ followed by the single character, and all others characters are written in #\u notation with four digits or #\U notation with eight digits (using the latter only if the character value it does not fit in four digits).
All characters display directly as themselves (i.e., a single character).
12.7.11 Printing Keywords
Keywords write and display the same as symbols, except (see Printing Symbols) with a leading #:, and without special handing for an initial # or when the printed form would matches a number or a delimited . (since #: distinguishes the keyword).
12.7.12 Printing Regular Expressions
Regexp values in both write and display mode print starting with #px (for pregexp-based regexps) or #rx (for regexp-based regexps) followed by the write form of the regexp’s source string or byte string.
12.7.13 Printing Unreadable Values
For any value with no other printing specification, the output form is #<〈something〉>, where 〈something〉 is specific to the type of the value and sometimes to the value itself.