If you have not done so already, scan the reading on preconditions and postconditions
a. Write the
all-real? procedure described in the
b. What preconditions should
c. Is it necessary to test those preconditions? Why or why not?
Revise the definition of
greatest-of-list given in the
corresponding reading so that
it prints a different (and appropriate) error message for each error
I'd recommend that you use
cond rather than
Revise the definition of the
count-from procedure presented in
the reading on
recursion with natural numbers so that it enforces the precondition
that its first argument be less than or equal to its second argument.
Here is a procedure that computes the product of all of the odd natural
numbers up to and including
(define odd-factorial (lambda (number) (if (= number 1) 1 (* number (odd-factorial (- number 2))))))
a. What precondition does
odd-factorial impose on its argument?
b. What will happen if this precondition is not met?
c. Revise the definition of
odd-factorial as a
husk-and-kernel program in which the husk enforces the precondition.
d. How can we be certain, in this case, that none of the calls we make to the kernel procedure violates the precondition?
a. Define and test a procedure named
index that takes a symbol
sym and a list
ls of symbols as its arguments and
returns the number of list elements that precede the first occurrence of
> (index 'gamma (list 'alpha 'beta 'gamma 'delta)) 2 > (index 'easy (list 'easy 'medium 'difficult 'impossible)) 0 > (index 'the (list 'and 'the 'cat 'sat 'on 'the 'mat)) 1
b. Arrange for
index to signal an error (by invoking the
error procedure) if
sym does not occur at all as
an element of
Define and test a procedure named
substitute that takes three
arguments -- a symbol
new, another symbol
and a list
ls of symbols -- and returns a list just like
ls except that every occurrence of
old has been
replaced with an occurrence of
new. Use the husk-and-kernel
structure to make sure that
symbols and that
ls is a list of symbols before starting into
> (substitute 'alpha 'omega (list 'phi 'chi 'psi 'omega 'omega) (phi chi psi alpha alpha) > (substitute 'starboard 'port (list 'port 'starboard 'port 'port)) (starboard starboard starboard starboard) > (substitution 'in 'out null) ()
Friday, 15 September 2000
Monday, 18 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/Labs/prepost.html
Source text last modified Mon Sep 18 11:58:46 2000.
This page generated on Mon Sep 18 11:59:34 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at email@example.com