Held Monday, September 25, 2000
Today we visit issues pertaining to procedures that may behave differently each time you run them.
(random val)which produces a ``random'' value between 0 and val-1, inclusive.
a. Define a procedure,
(roll), that simulates a die. It
should return a number between 1 and 6.
b. Define a procedure,
(pair-a-dice), that produces the result of
rolling two dice.
c. Define a procedure,
that counts the number of sevens in the given number of rolls of a
pair of dice dice.
d. Define a procedure,
(sevens-or-elevens) that rolls
two dice and returns true if they sum to seven and false otherwise.
Play with those three methods and see how they are similar and how they differ.
;;; do-some-stuff ;;; By Samuel A. Rebelsky ;;; Version 1.0 of 25 September 2000 ;;; Purpose: Prints some simple output ;;; Pre: None ;;; Post: Output has been generated ;;; Produces: Nothing (define do-some-stuff (lambda () (display "Hello") (newline) (display "Goodbye")))
a. Make sure that
b. What do you think will happen if you try
c. Try some other simple sequences of operations.
;;; interactive-square-root ;;; Prompts for a number and prints its square root ;;; By Samuel A. Rebelsky ;;; Version 1.0 of 25 September 2000 ;;; Parameters: None ;;; Preconditions: None ;;; Postconditons: Has read a number from input. Prints output. ;;; Produces: Nothing (define interactive-square-root (lambda () (display "Enter a number: ") (isr-helper (read)))) (define isr-helper (lambda (val) (display "The square root of ") (display val) (display " is ") (display (sqrt val)) (newline)))
a. Test this pair of procedures.
b. Make the pair of procedures repeatedly ask for a value and compute its square root. (Hint: use recursion.)
c. How do you stop your program?
d. Add a test that ensures that the value entered is a number and, if not, stops the program.
Thursday, 24 August 2000
Monday, 25 September 2000
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.18.html
Source text last modified Mon Sep 25 10:50:38 2000.
This page generated on Mon Sep 25 10:52:48 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at firstname.lastname@example.org