Version: 4.2.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)


  > (write #\x)


  > (write "hello")


  > (write #"goodbye")


  > (write '|dollar sign|)

  |dollar sign|

  > (write '("alphabet" soup))

  ("alphabet" soup)

  > (write write)



  > (display 1/2)


  > (display #\x)


  > (display "hello")


  > (display #"goodbye")


  > (display '|dollar sign|)

  dollar sign

  > (display '("alphabet" soup))

  (alphabet soup)

  > (display 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.


  (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.


  > (define-values (in out) (make-pipe))
  > (write "hello" out)
  > (read in)


  > (write '("alphabet" soup) out)
  > (read in)

  ("alphabet" soup)

  > (write #hash((b. "banana")              (a. "apple")) out)
  > (read in)

  #hash((b. "banana")              (a. "apple"))