This lab is also available in PDF.
Summary: In the laboratory, you will consider mechanisms for verifying the preconditions of procedures. You will also consider some issues int he documentation of such procedures.
a. Document and write the
all-real? procedure described in the accompanying reading.
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 condition.
I'd recommend that you use
cond rather than
if in writing this revised version.
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 or preconditions
odd-factorial impose on its argument?
b. What will happen if these preconditions are 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 recursive calls we make to the kernel procedure violates the precondition?
a. Document (using the six-P style), define, and test a procedure
index-of that takes a symbol
sym and a
ls of symbols as its arguments and returns the index
ls. You should use 0-based indices,
so that the initial value in a list is at index 0. (Note that we
defined a similar procedure in a recent homework assignment. I would
prefer that you not refer to the answer key in your work here.)
> (index-of 'gamma (list 'alpha 'beta 'gamma 'delta)) 2 > (index-of 'easy (list 'easy 'medium 'difficult 'impossible)) 0 > (index-of 'the (list 'and 'the 'cat 'sat 'on 'the 'mat)) 1
b. Arrange for
index-of to signal an error (by invoking the
error procedure) if
sym does not occur at all as
an element of
sym does not occur as an element of
ls, is it better to have for
index-of to invoke
error or return a special value (such as -1 or
#f)? Explain your answer.
Document (using the six P style), define, and test a procedure
substitute that takes three arguments -- a
new, another symbol
old, and a list
ls of symbols -- and returns a list just like
except that every occurrence of
old has been replaced with
an occurrence of
new. Use the husk-and-kernel structure to
make sure that
old are symbols and that
ls is a list of symbols before starting into the recursion.
> (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) > (substitute 'in 'out null) () > (substitute "in" 'out null) substitute: expected a symbol as first parameter > (substitute 'in 'out (list 'alpha "beta" 23)) substitute: expected a list of symbols as third parameter
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Nov 30 21:43:07 2006.
The source to the document was last modified on Fri Sep 22 08:47:53 2006.
This document may be found at
You may wish to validate this document's HTML ; ;Samuel A. Rebelsky, email@example.com
http://creativecommons.org/licenses/by-nc/2.5/or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.