| CSC 153 | Grinnell College | Spring, 2009 |
| Computer Science Fundamentals | ||
This laboratory exercise provides practice with several elements of program correctness and design:
State pre-conditions and post-conditions for procedures power, countdown, replicate and square-each-element from the lab on recursion.
Add tests of pre-conditions for procedures power and square-each-element from the lab on recursion.
Write a husk-and-kernel version of the replicate procedure
from the lab on recursion.
Write a procedure sum-of-list that takes any list of
numbers and returns the sum of of the elements of the list. Have the
procedure report an error if its parameter is not a list or if not all list
elements are numbers.
Define a predicate author? that takes one argument and
determines whether that argument is a list containing exactly three
arguments, the first one a string and the second and third ones integers.
(The predicate should return #t if all of these
conditions are met, #f if any one of them is not
satisfied.)
The reading for this lab included the following Scheme definitions. These are reproduced here for easy reference.
(define longest-on-list
(lambda (ls)
;Pre-condition: ls is non-empty list of strings
;Post-conditions: generates an error if pre-condition is not met
; otherwise, returns the string on ls of longest length
;; Check pre-conditions
(if (or (null? ls)
(not (list-of-strings? ls)))
(error "the argument must be a non-empty list of strings"))
;; Find the longest string on the list.
(longest-on-list-kernel ls)))
(define longest-on-list-kernel
(lambda (ls)
;Pre-condition: ls is non-empty list of strings
;Post-conditions: returns the string on ls of longest length
(if (null? (cdr ls))
(car ls)
(longer-string (car ls)
(longest-on-list-kernel (cdr ls))))))
(define longer-string
(lambda (str1 str2)
(if (< (string-length str1) (string-length str2))
str2
str1)))
(define list-of-strings?
(lambda (ls)
;Pre-condition: none
;Post-condition: returns #t if ls is a non-empty list of strings
(or (null? ls)
(and (pair? ls)
(string? (car ls))
(list-of-strings? (cdr ls))))))
Identify tests cases that will cover a full range of situations that might be encountered in executing the longest-on-string procedure.
Develop a test plan for the tally-by-parity problem from the lab on recursion.
At the start of your procedure definitions, add the line
(require (lib "trace.ss"))
Now type (trace longest-on-list) and (trace longest-on-list-kernel) into the interaction window and run several test cases. Describe the output obtained, and explain why it appears.
Turn off tracing for the husk and kernel procedures, with the statement (untrace longest-on-list) and (untrace longest-on-string-kernel). Now turn on tracing for longer-string. Run a few test cases, describe the output obtained, and explain why this output is obtained.
Notes
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/153.sp09/labs/lab-correctness.shtml
|
created 13 January 1998 by John David Stone and Henry
M. Walker last revised 1 April 2008 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |