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
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 nontrivial 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: ;; replaceeolwith : listofnumbers listofnumbers > listofnumbers ;; to construct a new list by replacing empty in alon1 with alon2 (define (replaceeolwith 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 replaceeolwith should produce the second argument, no matter what it is: (replaceeolwith 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 : (replaceeolwith (cons 1 empty) L) ;; expected value: (cons 1 L) (replaceeolwith (cons 2 (cons 1 empty)) L) ;; expected value: (cons 2 (cons 1 L)) (replaceeolwith (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/curriculumZH22.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. ;; replaceeolwith : listofnumbers listofnumbers > listofnumbers ;; to construct a new list by replacing empty in alon1 with alon2 (define (replaceeolwith alon1 alon2) (cond ((empty? alon1) alon2) (else (cons (first alon1) (replaceeolwith (rest alon1) alon2))))) Figure 45: The complete definition of replaceeolwith 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 listprocessing function with respect to the first argument: (define (replaceeolwith alon1 alon2) (cond ((empty? alon1) ...) (else ... (first alon1) ... (replaceeolwith (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
 Spring '07
 Fisler

Click to edit the document details