Exercise 1.2.10
Printing With prin1
The prin1
function is very similar to print
, except it doesn't output a preceding newline or a trailing space. Its emphasis is on printing readably for the Lisp Reader, i.e., what you print with prin1
is meant to be read again as source code. Like print
, prin1
sets the Lisp printer paramater :escape
to t
, so special characters are printed escaped in strings, the same way you have to type them at the REPL and in your Lisp source files.
(prin1 "Although largely uninteresting, this \"string\" has escaped characters in it.")
(prin1 "Remember, since the backslash (\\) is the escape character, it needs to be escaped too.")
(prin1 "\Y\o\u\ \c\a\n\ \a\l\s\o\ \e\s\c\a\p\e\ \e\v\e\r\y\ \c\h\a\r\a\c\t\e\r\ \i\n\ \a\ \s\t\r\i\n\g\.")
What You Should See
* (prin1 "Although largely uninteresting, this \"string\" has escaped characters in it.")
"Although largely uninteresting, this \"string\" has escaped characters in it."
"Although largely uninteresting, this \"string\" has escaped characters in it."
* (prin1 "Remember, since the backslash (\\) is the escape character, it needs to be escaped too.")
"Remember, since the backslash (\\) is the escape character, it needs to be escaped too."
"Remember, since the backslash (\\) is the escape character, it needs to be escaped too."
* (prin1 "\Y\o\u\ \c\a\n\ \a\l\s\o\ \e\s\c\a\p\e\ \e\v\e\r\y\ \c\h\a\r\a\c\t\e\r\ \i\n\ \a\ \s\t\r\i\n\g\.")
"You can also escape every character in a string."
"You can also escape every character in a string."
Notice how only the characters that need to be escaped in a string are printed and returned escaped? As far as the Lisp Reader is concerned, a string can contain any character---but since the double-quote character terminates a string, it needs to be escaped if you want it printed, and since the escape character used would normally escape the following character, you also have to escape the backslash to print it. But that's it. Every other character supported by your implementation's current encoding can be included in a string.