Fundamentals of Computer Science I (CS151 2003F)

Generating Extra Output

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



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.


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))
> my-input
> (write my-input)


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"
> (write #\A)


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")
    (display "This is on a ")
    (display "separate 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.



Tuesday, 5 November 2002 [Samuel A. Rebelsky]

Monday, 27 October 2003 [Samuel A. Rebelsky]


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Dec 9 14:00:22 2003.
The source to the document was last modified on Mon Oct 27 10:55:29 2003.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,