CSE 341 - Programming Languages - Autumn 2006
Scheme

Scheme I/O

Scheme includes various input and output statements, including terminal-based I/O and file I/O.

Here are a few I/O functions that we will need in 341. (There are others, of course ...)

Note that all of these have side effects!

read is a function that reads one Scheme object from the standard input and returns it.

Example:

(define clam (read))
There are some other functions to to read characters without parsing them into Scheme objects.

write prints a representation of its argument to the standard output.

Example. This outputs a 7:

(write (+ 3 4))
display is similar to write, but omits quotes around strings, and interprets C-style escape characters in the string correctly. For example:
(write " hi \n there")
prints:
" hi \n there"
while
(display " hi \n there")
prints:
 hi
 there
(newline) does the same thing as (display "\n").

Multiple Forms

Many of the constructs we've discussed so far, including define, let, and cond, allow multiple forms in their bodies. All but the last form is evaluated just for its side effect, and the value of the last form is used. For example:
(define (testit x)
  (write "this function doesn't do much")
  (newline)
  (write "but it does illustrate the point")
  (newline)
  (+ x x))
Here the write and newline expressions are evaluated (for their effect) and then the last expression (+ x x) is evaluated and its value returned as the value of the function. Similarly, there can be multiple forms in the body of a let, or the consequent part of a clause in a cond. However, if must have just a single form in its two arms. (Otherwise, how would Scheme tell which was which?)

This capability hasn't been of any use in the past, since we haven't had any side effects -- clearly, the only time one would want to use this is if the forms other than the last have some side effect.