[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
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
Overview
Print a prompt Read a value If that value is not a number Stop Otherwise Print the square root of that value Do it all over again
(display "Enter a number: ")
(read)
(not (number? value))
(display (sqrt value))
(interactive-square-root)
value
with the value we've read. How do we associate names with values?
define. (Not a great idea in this case.)
;;; 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: ")
(isr-helper (read))))
(define isr-helper
(lambda (value)
(if (not (number? value))
(quit)
...)))
;;; quit
(define quit
(lambda ()
(display "Bye!")
(newline)))
(define print-root
(lambda (value)
(display "The square root of ")
(display value)
(display " is ")
(display (sqrt value))
(newline)))
if?
begin to create a block of code. Here's the
format
(begin exp1 exp2 ... expn)
begin structure ...
(define isr-helper
(lambda (value)
(if (not (number? value))
(quit)
(begin
(print-root value)
(interactive-square-root)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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 (read))))
;;; 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)
introot.ss.
mzscheme -r introot.ss
(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.
Thursday, 24 August 2000
Tuesday, 26 September 2000
Back to Side effects, sequencing, and output. On to CGI scripting.
[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
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.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Outlines/outline.19.html
Source text last modified Tue Sep 26 10:51:09 2000.
This page generated on Tue Sep 26 10:56:15 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu