Section 31

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

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

View Full DocumentRight Arrow Icon
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 31 Designing Accumulator-Style Functions Section 30 illustrated with two examples the need for accumulating extra knowledge. In some cases, the accumulation makes it easier to understand a function; in others it is necessary for the function to work properly. In both cases, however, we first chose one of the available design recipes, inspected the function, and then revised or fixed it. Put more generally, adding an ACCUMULATOR, that is, a parameter that accumulates knowledge, is something that we add to a function after we have designed a function, not before. The keys to the development of an accumulator-style function are: 1. to recognize that the function benefits from, or needs, an accumulator; 2. to understand what the accumulator represents. The first two subsections address these two questions. Because the second one is a difficult topic, the third subsection illustrates how to formulate precise claims about accumulators. More concretely, in this section, we transform several standard recursive functions into functions that use auxiliary functions with accumulators. 31.1 Recognizing the Need for an Accumulator Recognizing the need for accumulators is not an easy task. We have seen two reasons, and they are the most prevalent reasons for adding accumulator parameters. In either case, it is critical that we first built a complete function based on a design recipe . Then we study the function and look for one of the following characteristics: 1. If the function is structurally recursive and if the result of a recursive application is processed by an auxiliary, recursive function, then we should consider the use of an accumulator parameter. Take the function invert as an example: ;; invert : (listof X) -> (listof X) ;; to construct the reverse of alox ;; structural recursion (define (invert alox) (cond [(empty? alox) empty] [else (make-last-item (first alox) (invert (rest alox)))])) ;; make-last-item : X (listof X) -> (listof X) ;; to add an-x to the end of alox ;; structural recursion file:///C|/Documents%20and%20Settings/Linda%20Graue...How%20to%20Design%20Programs/curriculum-Z-H-39.html (1 of 13) [2/5/2008 4:55:29 PM] How to Design Programs: An Introduction to Computing and Programming (define (make-last-item an-x alox) (cond [(empty? alox) (list an-x)] [else (cons (first alox) (make-last-item an-x (rest alox)))])) The result of the recursive application produces the reverse of the rest of the list. It is processed by make-last- item , which adds the first item to the reverse of the rest and thus creates the reverse of the entire list. This second, auxiliary function is also recursive. We have thus identified a potential candidate. It is now time to study some hand-evaluations, as we did in section 30.1 , to see whether an accumulator helps....
View Full Document

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

Page1 / 13

Section 31 - 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