Laboratory 17: Input and Output

Summary: In this laboratory, you will experiment with the use and application of some of Scheme's basic input and output procedures.

Contents


Exercises

Exercise 0: Preparation

a. Make sure that you understand what read, write, and display are supposed to do. You may find the reading on input and the reading on output helpful.

b. Ensure that you understand the sample code from the reading on input.

c. Start DrScheme.

Exercise 1: Practice with read

For each of part of this exercise, you should use something like

(define val (read))

to read in a value.

For example,

> (define val (read))
45
> val
45

a. Use read to obtain a number. That is, call read as above and enter a number.

b. Use read to obtain a string.

c. Use read to obtain a character.

d. Use read to obtain a list of numbers.

e. Use read to obtain a symbol. Verify that the value you obtained is a symbol using the symbol? predicate.

f. Use read to obtain a list that contains a number, a string, and a symbol.

g. What happens when you use read to obtain a list and you hit <Enter> in the middle of the list?

Exercise 2: Simple Input and Output

Consider the following sequence of Scheme commands:

(display "Please enter a value and I will square it: ")
(define val (read))
(define val-squared (* val val))
(display (string-append "The value of "
(number->string val)
" squared is "
(number->string val-squared)))
(newline)

a. What do you expect the code to do?

b. Verify your answer via experimentation.

Exercise 3: Local Input Values

Rewrite the code from the previous exercise to use let or let* (or both) rather than define.

Exercise 4: Error Checking

a. What happens in your square program if someone enters something other than a number?

b. Update your program so that it prints a friendly error message (using display) and then asks again if someone enters something other than a number.

Exercise 5: Computing Roots of a Quadratic Equation

a. Write a Scheme program that reads in the three coefficients of a quadratic equation (the a, b, and c in ax2 + bx + c) and prints out the roots of the equation. You should model this program on the previous exercises. In case you've forgotten, the roots of the quadratic equation are

(-b +/- sqrt(b2 - 4ac)) / 2a

b. Save the program in a file and execute it from the command line.

c. Reflect on what happened. Did you need to type any Scheme? Could someone else use step b without understanding the underlying Scheme?

Exercise 6: Repetition

Write a program repeatedly asks for a value and computes its square root. You will probably need to

a. Put the stuff (request for value, computation of a value, display of that value) in a procedure.

b. Have that procedure recurse.

c. Decide upon a base case to stop recursion. (I'd suggest that you stop when someone enters something other than a number.)

For those with extra time

Running from the Command Line

a. Save the code from exercise 6 in a file named square.scm.

b. Open a terminal window.

c. In that terminal window, type

mzscheme -r square.scm

d. Reflect on what happened in step c. Did you need to type any Scheme? Could someone else use step c without understanding the underlying Scheme?

e. Create a file called square that contains the following lines

#!/bin/bash
/usr/bin/mzscheme -r square.scm

f. In the terminal window, type

chmod 755 square

g. In the terminal window, type

square

h. Reflect on what just happened.


Janet Davis (davisjan@cs.grinnell.edu)

Created February 22, 2007 based on http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Labs/io.html
Last revised February 22
, 2007
With thanks to Sam Rebelsky