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

 

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:

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