Version: 4.1

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