Class 19: Scheme without DrScheme

Back to Side effects, sequencing, and output. On to CGI scripting.

Held Tuesday, September 26, 2000

Summary

Today we continue our exploration of interactive Scheme programs by seeing how to write Scheme programs that run without DrScheme.

Notes

• Are there questions on the exam?
• Don't beat your head against the wall for any problem; if you're experiencing difficulties, come see me.
• We'll talk about parts of yesterday's assignment today.
• Those of you who want to use the new, Web-based plan alternative, send email to heckr@grinnell.edu.

Overview

• Interactive square root, continued
• Schemeing without DrScheme
• A database application

An Interactive Square Root, Revisited

• Let's go over the last problem from yesterday's lab.
• Our goal is to create an interactive procedure that repeatedly reads numbers and prints square roots. It quits when the user enters something other than a number.
• Here's the basic structure:
```Print a prompt
If that value is not a number
Stop
Otherwise
Print the square root of that value
Do it all over again
```
• We can do some parts easily.
• Print a prompt
`(display "Enter a number: ")`
`(read)`
• Determine if a value is not a number
`(not (number? value))`
• Print the square root
`(display (sqrt value))`
• Do it again `(interactive-square-root)`
• But we have a problem: We want to associate the name `value` with the value we've read. How do we associate names with values?
• Use `define`. (Not a great idea in this case.)
• Call a procedure. All the parameters to the procedure serve as names.
• So, here's what I get
```;;; interactive-square-root
;;;   Repeatedly prompts for a number and prints its square root
;;; By Samuel A. Rebelsky
;;; Version 1.1 of 26 September 2000
;;; Parameters: None
;;; Preconditions: None
;;; Postconditions: Has read values from input.  Prints output.
;;; Produces: Nothing
(define interactive-square-root
(lambda ()
(display "Enter a number: ")

(define isr-helper
(lambda (value)
(if (not (number? value))
(quit)
...)))
```
• How do we quit? We print a goodbye message.
```;;; quit
(define quit
(lambda ()
(display "Bye!")
(newline)))
```
• We know how to display a square root.
```(define print-root
(lambda (value)
(display "The square root of ")
(display value)
(display " is ")
(display (sqrt value))
(newline)))
```
• But how do we put that together into the last part of the `if`?
• Using `begin` to create a block of code. Here's the format
```(begin
exp1
exp2
...
expn)
```
• This evaluates expression 1 through expression n in order and returns the value of expression n.
• So let's use the `begin` structure ...
```(define isr-helper
(lambda (value)
(if (not (number? value))
(quit)
(begin
(print-root value)
(interactive-square-root)))))
```
• Putting it all together, we get the following (stored in introot.ss)
```;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; root.ss
;;;   A scheme program that prompts for values and prints
;;;   their square roots.
;;; Author:  Samuel A. Rebelsky
;;; Version: 1.0
;;; Created: Tuesday, 26 September 2000
;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Procedures

;;; interactive-square-root
;;;   Repeatedly prompts for a number and prints its square root
;;; Parameters: None
;;; Preconditions: None
;;; Postconditions: Has read values from input.  Prints output.
;;; Produces: Nothing
(define interactive-square-root
(lambda ()
(display "Enter a number (or quit to quit): ")

;;; isr-helper
;;;   Helper procedure for interactive-square-root
;;;   Checks the value entered and does something appropriate
;;; Parameters: The value entered
(define isr-helper
(lambda (val)
(if (not (number? val))
(quit)
(begin
(display "The square root of ")
(display val)
(display " is ")
(display (sqrt val))
(newline)
(interactive-square-root)))))

;;; quit
;;;   Display a goodbye message.  (Doesn't really quit.)
;;; Parameters: None
;;; Preconditions: None
;;; Postconditions: Has displayed text.
;;; Produces: Nothing
(define quit
(lambda ()
(display "Bye!")
(newline)))

;;; print-root
;;;   Displays information about the square root of a value
;;; Parameters: A value
;;; Preconditions: The value is a number.  (NOT CHECKED!)
;;; Postconditions: Has displayed helpful text.
;;; Produces: Nothing
(define print-root
(lambda (value)
(display "The square root of ")
(display value)
(display " is ")
(display (sqrt value))
(newline)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Run the program

(interactive-square-root)
```
• Try running this in DrScheme.

Working Without DrScheme

• Can we run this program without using DrScheme, so that it seems like a ``normal'' program? Yes!
• Try the following

A Simple Application

• Let's try to write a similar application that prompts the user for a person's name and prints information on that person.
• We'll start in DrScheme and then move to MzScheme.
• You can use an association list to help. each person, we'll store the list (name haircolor eyecolor favecolor)
• For example
```(define people
'((John Brown Green Yellow)
(Jack Blonde Blue Purple)
(Jane Black Black Black)))
```

a. Write a procedure,

`(summarize-person list)`

that uses `display` to print a helpful summary of a person.

b. Test your procedure using `'(SamR Dark Brown Plaid)`

c. Test your procedure using `(assoc 'John people)`

d. Write a procedure that reads in a person's name and prints out a summary.

e. Turn all of your stuff into a Scheme .ss file.

f. Run it using MzScheme.

History

Thursday, 24 August 2000

• Created as a blank outline.

Tuesday, 26 September 2000

• Filled in the details.

Back to Side effects, sequencing, and output. On to CGI scripting.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.