Guide: PLT Scheme

1`` | Welcome to PLT Scheme
2`` | Scheme Essentials
3`` | Built-In Datatypes
4`` | Expressions and Definitions
5`` | Programmer-Defined Datatypes
6`` | Modules
7`` | Contracts
8`` | Input and Output
9`` | Regular Expressions
10`` | Exceptions and Control
11`` | Iterations and Comprehensions
12`` | Pattern Matching
13`` | Classes and Objects
14`` | Units (Components)
15`` | Reflection and Dynamic Evaluation
16`` | Macros
17`` | Performance
18`` | Running and Creating Executables
19`` | Compilation and Configuration
20`` | More Libraries
| Bibliography
| Index

Version: 4.0.2
contents index
← prev up next →

#### 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:

> (write 1/2)
1/2
> (write #\x)
#\x
> (write "hello")
"hello"
> (write #"goodbye")
#"goodbye"
> (write '|dollar sign|)
|dollar sign|
> (write '("alphabet" soup))
("alphabet" soup)
> (write write)
#<procedure:write>

> (display 1/2)
1/2
> (display #\x)
x
> (display "hello")
hello
> (display #"goodbye")
goodbye
> (display '|dollar sign|)
dollar sign
> (display '("alphabet" soup))
(alphabet soup)
> (display write)
#<procedure:write>

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:

(define (deliver who what)
(printf "Value for ~a: ~s" who what))

> (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"))

contents index
← prev up next →