Outside the interface

The DrScheme system is a development environment -- a collection of tools to assist programmers. It is built around a Scheme processor named MzScheme, which is the component that actually evaluates your expressions and executes your programs.

When someone wants to execute a fully developed Scheme program, she can invoke the MzScheme processor directly, without bothering to call up the rest of the DrScheme environment. For instance, consider the following Scheme program, reminiscent of the square-root-computer procedure from the lab on input and output procedures, but offering the user the possibility of recovering from incorrect input:

;;; Square root program

;;; John David Stone
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; stone@cs.grinnell.edu

;;; created March 5, 2000
last revised March 17, 2000

;;; The PROMPT procedure takes the text of a prompt as its argument,
;;; displays it, and collects and returns the user's reply.

(define prompt
  (lambda (prompt-text)
    (display prompt-text)
    (read)))

;;; The PROMPT-FOR-NUMBER procedure prompts the user for a number, which it 
;;; returns.  If the user supplies something that is not a number,
;;; PROMPT-FOR-NUMBER displays an advisory message and repeats the prompt.

(define prompt-for-number
  (lambda ()
    (let ((candidate (prompt "Number: ")))
      (if (number? candidate)
          candidate
          (begin
            (display "The value ")
            (write candidate)
            (display " is not a number.")
            (newline)
            (display "Please supply a number.")
            (newline)
            (prompt-for-number))))))

;;; The REPORT-SQUARE-ROOT procedure takes a number as its argument and
;;; displays the square root of that number as part of an English sentence
;;; that describes it.

(define report-square-root
  (lambda (number)
    (display "The square root of ")
    (display number)
    (display " is ")
    (display (sqrt number))
    (display ".")
    (newline)))

;;; So much for definitions.  The executable part of the program begins
;;; here.

;;; Explain to the user what the program does.

(display "Give me a number, and I'll compute its square root.")
(newline)

;;; Get a number from the user and report its square root.

(report-square-root (prompt-for-number))

The file /home/stone/courses/scheme/examples/square-root-program.ss contains a copy of this program. You can ask MzScheme to run it by creating a terminal-emulator window and giving the command

mzscheme -r /home/stone/courses/scheme/examples/square-root-program.ss

at the shell prompt. MzScheme learns the three definitions at the top of the program and then evaluates the procedure calls that follow, much as if they had been typed into a DrScheme interactions window. Instead of drawing an interaction box around the program's input and output, however, MzScheme simply displays it in the terminal-emulator window:

bourbaki% mzscheme -r /home/stone/courses/scheme/examples/square-root-program.ss
Give me a number, and I'll compute its square root.
Number: 9604
The square root of 9604 is 98.

When it reaches the end of the program, MzScheme shuts itself down, returning control of the terminal-emulator window to the shell.

When you're running a program under MzScheme rather than DrScheme, there is no Break button to interrupt a runaway recursion. However, pressing <Control/C> in the terminal-emulator window has the same effect.


Exercise 1

Copy the square-root program into your home directory by starting a terminal-emulator window and giving the command

cp /home/stone/courses/scheme/examples/square-root-program.ss square-root-program.ss

at the shell prompt. Have MzScheme run your copy by giving the command

mzscheme -r square-root-program.ss

at the shell prompt. What happens if you give the program some value that is not a number? Why?


Exercise 2

Start DrScheme and create a similar program, iota-program, that prompts the user for a natural number n and displays the value of (iota n) in a descriptive English sentence. Store your program in a file named iota-program.ss.

Have MzScheme run your program by giving the command

mzscheme -r iota-program.ss

at the shell prompt.


Exercise 3

Using the sum-of-inputs procedure that you wrote as a solution to exercise 4 of the lab on input and output procedures, write a calculator program that takes as many numbers as the user types in and returns their sum.


Exercise 4

It is also possible to run the MzScheme processor without giving it a program to run, just by typing

mzscheme

by itself at the shell prompt. What does MzScheme do when it has no program to follow? Could this behavior ever be useful?

(Incidentally, you can shut down MzScheme by pressing <Control/D>.)


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~stone/courses/scheme/outside-the-interface.xhtml

created March 5, 2000
last revised March 17, 2000

John David Stone (stone@cs.grinnell.edu)