Section 29

How to Design Programs: An Introduction to Programming and Computing

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

View Full Document Right Arrow Icon
How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 29 Intermezzo 5: The Cost of Computing and Vectors In section 26.3 we discussed the differences between a structurally recursive program and an equivalent, generative version. The comparison revealed that the generative one is much faster than the structural version. We used both informal arguments, using the number of recursive calls, and measurements, using time expressions (exercises 26.3.1 and 26.3.3 ), to support our conclusion. While timing the application of a program to specific arguments can help us understand a program's behavior in one situation, it is not a fully convincing argument. After all, applying the same program to some other inputs may require a radically different amount of time. In short, timing programs for specific inputs has the same status as testing programs for specific examples. Just as testing may reveal bugs, timing may reveal anomalies concerning the execution behavior for specific inputs. It does not provide a firm foundation for general statements about the behavior of a program. This intermezzo introduces a tool for making general statements about the time that programs take to compute a result. The first subsection motivates the tool and illustrates it with several examples, though on an informal basis. The second one provides a rigorous definition. The last one uses the tool to motivate an additional class of Scheme data and some of its basic operations. 29.1 Concrete Time, Abstract Time Let's study the behavior of how-many , a function that we understand well: (define (how-many a-list) (cond [(empty? a-list) 0] [else (+ (how-many (rest a-list)) 1)])) It consumes a list and computes how many items the list contains. Here is a sample evaluation: (how-many (list 'a 'b 'c)) = (+ (how-many (list 'b 'c)) 1) file:///C|/Documents%20and%20Settings/Linda%20Graue. ..How%20to%20Design%20Programs/curriculum-Z-H-36.html (1 of 20) [2/5/2008 4:54:28 PM]
Background image of page 1

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

View Full DocumentRight Arrow Icon
How to Design Programs: An Introduction to Computing and Programming = (+ (+ (how-many (list 'c)) 1) 1) = (+ (+ (+ (how-many empty) 1) 1) 1) = 3 It consists of only those steps that are natural recursions. The steps in between are always the same. For example, to get from the original application to the first natural recursion, we go through the following steps: (how-many (list 'a 'b 'c)) = (cond [(empty? (list 'a 'b 'c)) 0] [else (+ (how-many (rest (list 'a 'b 'c))) 1)]) = (cond [false 0] [else (+ (how-many (rest (list 'a 'b 'c))) 1)]) = (cond [else (+ (how-many (rest (list 'a 'b 'c))) 1)]) = (+ (how-many (rest (list 'a 'b 'c))) 1) The steps between the remaing natural recursions differ only as far as the substitution for a-list is concerned. If we apply
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

Section 29 - How to Design Programs: An Introduction to...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online