|CSC 153||Grinnell College||Spring, 2005|
|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
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 print a warning message if it is given an empty list. (The
procedure should return 0 after issuing this warning message.)
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
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: returns 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 'longest-on-list "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.
Replace define by trace-define in the definition of longest-on-list and longest-on-string-kernel in the code above, and then run several test cases. Describe the output obtained, and explain why it appears.
Revise longest-on-list and longest-on-string-kernel so they are defined with define rather than by trace-define. Then edit longer-string, so it is defined with trace-define. Run a few test cases, describe the output obtained, and explain why this output is obtained.
This document is available on the World Wide Web as
created January 13, 1998 by John David Stone and Henry
last revised January 22, 2005
|For more information, please contact Henry M. Walker at firstname.lastname@example.org.|