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-char. We discuss all but
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
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 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
> (display "sample string") sample string > (write "sample string") "sample string" > (DISPLAY #\A) A > (write #\A) #\A
newline procedure takes no arguments and returns an
unspecified value; as a side effect, it terminates the current output line.
Successive calls to
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.
(newline) has exactly the same effect as
(display #\newline), for which you can consider it a
Tuesday, 5 November 2002 [Samuel A. Rebelsky]
Input and output under program control, version of 25 October 2001, available at
Monday, 27 October 2003 [Samuel A. Rebelsky]
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