Section 30

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 30 The Loss of Knowledge When we design recursive functions, we don't think about the context of their use. Whether they are applied for the first time or whether they are called for the hundredth time in a recursive manner doesn't matter. They are to work according to their purpose statement, and that's all we need to know as we design the bodies of the functions. Altough this principle of context-independence greatly facilitates the development of functions, it also causes occasional problems. In this section, we illustrate the most important problem with two examples. Both concern the loss of knowledge that occurs during a recursive evaluation. The first subsection shows how this loss makes a structurally recursive function more complicated and less efficient than necessary; the second one shows how the loss of knowledge causes a fatal flaw in an algorithm. 30.1 A Problem with Structural Processing Suppose we are given the relative distances between a series of points, starting at the origin, and suppose we are to compute the absolute distances from the origin. For example, we might be given a line such as this: relative distance Each number specifies the distance between two dots. What we need is the following picture, where each dot is annotated with the distance to the left-most dot: absolute distance ;; relative-2-absolute : (listof number) -> (listof number) ;; to convert a list of relative distances to a list of absolute distances ;; the first item on the list represents the distance to the origin (define (relative-2-absolute alon) (cond [(empty? alon) empty] [else (cons (first alon) (add-to-each (first alon) (relative-2-absolute (rest alon))))])) ;; add-to-each : number (listof number) -> (listof number) ;; to add n to each number on alon (define (add-to-each n alon) (cond [(empty? alon) empty] [else (cons (+ (first alon) n) (add-to-each n (rest alon)))])) Figure 83: Converting relative distances to absolute distances file:///C|/Documents%20and%20Settings/Linda%20Grauer. ../How%20to%20Design%20Programs/curriculum-Z-H-38.html (1 of 9) [2/5/2008 4:54:54 PM]
Background image of page 1

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

View Full Document Right Arrow Icon
How to Design Programs: An Introduction to Computing and Programming Developing a program that performs this calculation is at this point an exercise in structural function design. Figure 83 contains the complete Scheme program. When the given list is not empty , the natural recursion computes the absolute distance of the remainder of the dots to the first item on (rest alon) . Because the first item is not the actual origin and has a distance of (first alon) to the origin, we must add (first alon) to each and every item on the result of the recursive application. This second step, adding a number to each item on a list of numbers, requires an auxiliary function.
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.

{[ snackBarMessage ]}

Page1 / 9

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