8.3 Reading and Writing Scheme Data
As noted throughout Built-In Datatypes, Scheme provides two ways to print an instance of a built-in value:
write, which prints a value in the same way that is it printed for a REPL result; 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:
|
|
|
The printf function supports simple formatting of data and text. In the format string supplied to printf, ~a displays the next argument, while ~s writes the next argument.
Examples: | |||
| |||
> (deliver "John" "string") | |||
Value for John: "string" |
An advantage of write, as opposed to display, is that many forms of data can be read back in using read.
Examples: |
> (define-values (in out) (make-pipe)) |
> (write "hello" out) |
> (read in) |
"hello" |
> (write '("alphabet" soup) out) |
> (read in) |
("alphabet" soup) |
> (write #hash((a . "apple") (b . "banana")) out) |
> (read in) |
#hash((a . "apple") (b . "banana")) |