Reading 17a: Generating Extra Output

Summary: We consider ways in which to have Scheme write some output (in addition to generating responses).

Contents:


Introduction

The Scheme model we have worked with so far is both simple and straightforward: The user types a Scheme expression, the computer thinks for awhile, and then prints the value of the the expression. However, some programs may benefit from additional output printed while the program is computing. For example, one helpful technique for understanding recursive procedures is to print out the current call at each step. More importantly, output procedures (along with corresponding input procedures) permit programmers to write programs that interact with the user.

Scheme provides four basic output operations: write, display, newline, and write-char. We discuss all but write-char below.

write

The write procedure takes one argument and prints out a representation of that argument. The nature of the value that it returns is unspecified (under DrScheme, for instance, it's the special void value) -- the printing is a side effect of the evaluation of the call to write, not its result.

DrScheme also encloses the material that write prints out inside an interaction box. You can distinguish user input from program output in an interaction box by its color: User input is displayed in green, program output in purple. Both are distinguished from DrScheme's usual way of exhibiting the value of an expression, which is to print it in dark blue without drawing an interaction box.

> (define my-input (read))
116
> my-input
116
> (write my-input)
116

display

The display procedure also takes one argument and prints out a representation of it, but it differs from write in that it does not enclose the representations of strings in double quotation marks and does not print the mesh-backslash combination when displaying a character:

> (display "sample string")
sample string
> (write "sample string")
"sample string"
> (DISPLAY #\A)
A
> (write #\A)
#\A

newline

The newline procedure takes no arguments and returns an unspecified value; as a side effect, it terminates the current output line. Successive calls to write and display normally produce output that is all strung together on one line. Calls to newline are used to break up such output into separate lines.

> (begin
(display "all-")
(display "on-")
(display "one-")
(display "line")
(newline)
(display "This is on a ")
(display "separate line.")
(newline))

all-on-one-line
This is on a separate line.

The call (newline) has exactly the same effect as (display #\newline), for which you can consider it a convenient shorthand.

Note that begin is also a useful keyword. When DrScheme sees a begin expression, it evaluates each of the expressions inside one at a time until it reaches the end.  (So, here is another way of sequencing operations.) However, DrScheme will return only the value of the last expression. In this example, (newline) doesn't return a value, so neither does the begin expression.


Janet Davis (davisjan@cs.grinnell.edu)

Created February 22, 2007 based on http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Readings/output.html
Last revised 
February 22, 2007
With thanks to Sam Rebelsky