Input and output under program control

Course links

Input and output procedures

In the programs we've written so far this semester, we've assumed that all the data that a program needs can be either included in the source code, generated automatically within the program, or (at worst) supplied in the interactions window as an argument in a call to one of the program's procedures.

Unfortunately, this simplifying assumption doesn't always hold. In many cases, we'd like our program to take over the job of interacting with users, reading in values and displaying results. To support programs of this kind, Scheme provides several primitive procedures that perform interactive input or output as ``side effects.'' In this lab, we'll study four of them:

Here's a small illustration of the use of the read procedure. The square-root-computer procedure asks the user to supply a number, computes the square root of the number that the user supplies, and prints out the result, appropriately labelled, all within the interaction box:

;;; square-root-computer: prompts the user for a number and
;;; outputs its square root

;;; Givens:
;;;   None (the number is read in interactively).

;;; Results:
;;;   None (the root is printed out, not returned as a value).

;;; Preconditions:
;;;   None.

;;; Postconditions:
;;;   (1) The program user has been prompted for a number.
;;;   (2) The program user's reply has been read in.
;;;   (3) If the program user's reply is a number, its square
;;;       root has been printed out, appropriately labelled.

(define square-root-computer
  (lambda ()
    (display "Give me a number, and I'll compute its square root.")
    (newline)
    (let ((proposed-number (begin
                             (display "Number: ")
                             (read))))
      (if (number? proposed-number)
          (begin
            (display "The square root of ")
            (display proposed-number)
            (display " is ")
            (display (sqrt proposed-number))
            (display ".")
            (newline))
          (error "square-root-computer: The input must be a number.")))))

The following sample calls demonstrate the working of the square-root-computer procedure. Notice that the value of proposed-number is not supplied as an argument to square-root-computer, but is read in as the program is being executed. The green printing shows where the user typed it in.

interactions with SQUARE-ROOT-COMPUTER

Sentinels

If one wants the procedure to compute many square roots instead of just one, prompting the user each time for a new number, one can set up a recursion in which the completion of each exchange initiates another:

;;; multi-square-root-computer: prompts the user for numbers and
;;; outputs the square root of each one

;;; Givens:
;;;   None.

;;; Results:
;;;   None.

;;; Preconditions:
;;;   None.

;;; Postconditions:
;;;   (1) The program user has been prompted at least once for
;;;       a number.
;;;   (2) The program user's reply has been read in.
;;;   (3) If the program user's reply is a number, its square
;;;       root has been printed out, appropriately labelled,
;;;       and the prompt has been repeated.
;;;   (4) If the program user's reply is the symbol STOP, a
;;;       cheerful salutation of farewell has been printed out
;;;       and the prompt has not been repeated.

(define multi-square-root-computer
  (lambda ()
    (display "Give me one number at a time.")
    (newline)
    (display "I'll compute its square root and then ask you for another number.")
    (newline)
    (display "Type STOP when you're done.")
    (newline)
    (let kernel ((proposed-number (begin
                                    (display "Number: ")
                                    (read))))
      (cond ((eq? proposed-number 'stop)
             (begin
               (display "Goodbye!")
               (newline)))
            ((number? proposed-number)
             (begin
               (display "The square root of ")
               (display proposed-number)
               (display " is ")
               (display (sqrt proposed-number))
               (display ".")
               (newline)
               (kernel (begin
                         (display "Number: ")
                         (read)))))
            (else
             (error "multi-square-root-computer: The input must be a number."))))))

Let's walk through the body of this procedure definition. When multi-square-root-computer is invoked, it begins by printing out three lines of instructions, then enters the recursive kernel, reading in the first user input as it enters and associating the parameter proposed-number with it.

The cond-expression first checks to see whether the user has submitted the symbol stop, which it interprets as a sentinel -- a conventional signal of the end of the input, indicating that the user is ready to leave the program. If the sentinel is detected, multi-square-root-computer prints out ``Goodbye!'' and returns.

If the user's input is not stop, however, the second cond-clause is activated. If the user has submitted a number, multi-square-root-computer figures its square root and displays the result, embedded in a complete English sentence.

On the other hand, if the user's input is neither the symbol stop nor a number, it is erroneous, and the procedure signals that a precondition has failed by invoking the error procedure to halt execution.

an interaction with MULTI-SQUARE-ROOT-COMPUTER

I am indebted to Professor Ben Gum for his contributions to the development of this reading.