As noted throughout Built-In Datatypes, Racket provides three ways to print an instance of a built-in value:
print , which prints a value in the same way that is it printed for a REPL result; and
write , which prints a value in such a way that read on the output produces the value back; and
display , which tends to reduce a value to just its character or byte content—at least for those datatypes that are primarily about characters or bytes, otherwise it falls back to the same output as write .
Here are some examples using each:
1/2
#\x
"hello"
#"goodbye"
'|pea pod|
'("i" pod)
#<procedure:write>
1/2
#\x
"hello"
#"goodbye"
|pea pod|
("i" pod)
#<procedure:write>
1/2
x
hello
goodbye
pea pod
(i pod)
#<procedure:write>
Overall, print corresponds to the expression layer of Racket syntax, write corresponds to the reader layer, and display roughly corresponds to the character layer.
The printf function supports simple formatting of data and text. In the format string supplied to printf , ~a display s the next argument, ~s write s the next argument, and ~v print s the next argument.
> (deliver'("list")'("John")'("milk"))Items (list) for shopper ("John"): '("milk")
After using write , as opposed to display or print , many forms of data can be read back in using read . The same values print ed can also be parsed by read , but the result may have extra quote forms, since a print ed form is meant to be read like an expression.
"hello"
'("alphabet" soup)
'#hash((a . "apple") (b . "banana"))
''("alphabet" soup)
'(alphabet soup)