Computer Science Fundamentals (CS153 2004S)

Preconditions and Postconditions

Summary: In this laboratory, you will consider various aspects of preconditions and postconditions. In particular, you will consider procedures for verifying preconditions, develop write procedures that check their preconditions, and write 6P-style documentation for some procedures.


Related Pages:


Exercise 0: Preparation

Start DrScheme.

Exercise 1: Are They All Real?

a. Write the all-real? procedure described in the accompanying reading.

b. What preconditions should all-real? have?

c. Is it necessary to test those preconditions? Why or why not?

Exercise 2: Differentiating Between Errors

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 recommend that you use cond rather than if in writing this revised version.

Exercise 3: When Can You Count Between?

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.

Exercise 4: An Odd Factorial

Here is a procedure that computes the product of all of the odd natural numbers up to and including number:

(define odd-factorial
  (lambda (number)
    (if (= number 1)
        (* number (odd-factorial (- number 2))))))

a. What precondition or preconditions does 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?

Exercise 5: Finding Values

a. Define (using the six-P style), implement, and test a procedure named index that takes a symbol sym and a list ls of symbols as its arguments and returns the index of sym in ls. You should use 0-based indices (so that the initial value in a list is at index 0).

> (index 'gamma (list 'alpha 'beta 'gamma 'delta))
> (index 'easy (list 'easy 'medium 'difficult 'impossible))
> (index 'the (list 'and 'the 'cat 'sat 'on 'the 'mat))

b. Arrange for index to signal an error (by invoking the error procedure) if sym does not occur at all as an element of ls.

Exercise 6: Substitution

Define (using the six P style), implement, and test a procedure named substitute that takes three arguments -- a symbol new, another symbol old, 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 new and 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



Friday, 15 September 2000 [Samuel A. Rebelsky]

Monday, 18 September 2000 [Samuel A. Rebelsky]

Thursday, 22 February 2001 [Samuel A. Rebelsky]

Sunday, 4 March 2001 [Samuel A. Rebelsky]

Thursday, 10 October 2002 [Samuel A. Rebelsky]

Tuesday, 4 February 2003 [Samuel A. Rebelsky]

Wednesday, 4 February 2004 [Samuel A. Rebelsky]


Disclaimer: 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 Fri May 7 09:44:20 2004.
The source to the document was last modified on Wed Feb 4 09:35:53 2004.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,