Fundamentals of CS I (CS151 2001S)
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Start DrScheme.
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?
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 number:
(define odd-factorial
(lambda (number)
(if (= number 1)
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?
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 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)) 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 ls.
Define 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) > (substitution 'in 'out null) ()
Friday, 15 September 2000 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~stone/courses/scheme/preconditions-and-postconditions.xhtml
Monday, 18 September 2000 [Samuel A. Rebelsky]
Thursday, 22 February 2001 [Samuel A. Rebelsky]
Sunday, 4 March 2001 [Samuel A. Rebelsky]
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Thu May 3 23:07:57 2001.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/prepost.html.
You may validate
this page's HTML.
The source was last modified Sun Mar 4 23:35:30 2001.