Fundamentals of Computer Science 1 (CS151 2003S)

Generating Extra Output

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

Contents:

Introduction

Still to be written.

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.

 

History

Tuesday, 5 November 2002 [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 May 6 09:30:58 2003.
The source to the document was last modified on Mon Mar 3 22:24:11 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Readings/output.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu