Section 17

# How to Design Programs: An Introduction to Programming and Computing

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 17 Processing Two Complex Pieces of Data On occasion, a function consumes two arguments that belong to classes with non-trivial data definitions. In some cases, one of the arguments should be treated as if it were atomic; a precisely formulated purpose statement typically clarifies this. In other cases, the two arguments must be processed in lockstep. Finally, in a few rare cases, the function must take into account all possible cases and process the arguments accordingly. This section illustrates the three cases with examples and provides an augmented design recipe for the last one. The last section discusses the equality of compound data and its relationship to testing; it is essential for automating test suites for functions. 17.1 Processing Two Lists Simultaneously: Case 1 Consider the following contract, purpose statement, and header: ;; replace-eol-with : list-of-numbers list-of-numbers -> list-of-numbers ;; to construct a new list by replacing empty in alon1 with alon2 (define (replace-eol-with alon1 alon2) ...) The contract says that the function consumes two lists, which we haven't seen in the past. Let's see how the design recipe works in this case. First, we make up examples. Suppose the first input is empty . Then replace-eol-with should produce the second argument, no matter what it is: (replace-eol-with empty L) = L In this equation, L stands for an arbitrary list of numbers. Now suppose the first argument is not empty . Then the purpose statement requires that we replace empty at the end of alon1 with alon2 : (replace-eol-with (cons 1 empty) L) ;; expected value: (cons 1 L) (replace-eol-with (cons 2 (cons 1 empty)) L) ;; expected value: (cons 2 (cons 1 L)) (replace-eol-with (cons 2 (cons 11 (cons 1 empty))) L) ;; expected value: (cons 2 (cons 11 (cons 1 L))) file:///C|/Documents%20and%20Settings/Linda%20Graue...How%20to%20Design%20Programs/curriculum-Z-H-22.html (1 of 26) [2/5/2008 4:48:38 PM] How to Design Programs: An Introduction to Computing and Programming Again, L stands for any list of numbers in these examples. ;; replace-eol-with : list-of-numbers list-of-numbers -> list-of-numbers ;; to construct a new list by replacing empty in alon1 with alon2 (define (replace-eol-with alon1 alon2) (cond ((empty? alon1) alon2) (else (cons (first alon1) (replace-eol-with (rest alon1) alon2))))) Figure 45: The complete definition of replace-eol-with The examples suggest that it doesn't matter what the second argument is -- as long as it is a list; otherwise, it doesn't even make sense to replace empty with the second argument. This implies that the template should be that of a list-processing function with respect to the first argument: (define (replace-eol-with alon1 alon2) (cond ((empty? alon1) ...) (else ... (first alon1) ... (replace-eol-with (rest alon1) alon2) ... ))) The second argument is treated as it were an atomic piece of data. The second argument is treated as it were an atomic piece of data....
View Full Document

## This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Spring '07 term at WPI.

### Page1 / 26

Section 17 - How to Design Programs An Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online