This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Accumulators Readings: Sections 30, 31, 32. Intermezzo 6, The Nature of Inexact Numbers, is interesting and important material, but will not be covered in this course. It is still worth reading. You will see a more indepth treatment in CS 251. CS 135 Fall 2008 10: Accumulators 1 Loss of knowledge At times during recursive computation, an application deep in a recursive chain might suffer from knowledge lost during earlier calls. One example we have already seen is backtracking: when the directed graph contains a cycle, backtracking may visit a node more than once. At the time of the second visit, the knowledge that there was an earlier visit has been lost. CS 135 Fall 2008 10: Accumulators 2 Running totals Suppose we have a list of numbers and we wish to form the list of the same length that contains the running totals. Example: ( list 2 6 4 3 ) has running totals ( list 2 8 12 15 ) since 8 = 2 + 6 , 12 = 2 + 6 + 4 , and 15 = 2 + 6 + 4 + 3 . We can design this function using structural recursion, but the resulting function is inefficient. CS 135 Fall 2008 10: Accumulators 3 If we form the running totals of ( rest ( list 2 6 4 3 )) , we get ( list 6 10 13 ) . The answer is ( list 2 8 12 15 ) . Adding 2 to each of these gets us the rest of the answer. This suggests an auxiliary function that adds a number to each element of a list of numbers. This can also be done using structural recursion. CS 135 Fall 2008 10: Accumulators 4 ;; total1:(listof num) (listof num) ( define ( total1 alon ) ( cond [( empty? alon ) empty ] [ else ( cons ( first alon ) ( addtoeach ( first alon ) ( total1 ( rest alon ))))])) ;; addtoeach: num (listof num) (listof num) ( define ( addtoeach m alon ) ( cond [( empty? alon ) empty ] [ else ( cons ( + m ( first alon )) ( addtoeach m ( rest alon )))])) CS 135 Fall 2008 10: Accumulators 5 Analysis of total1 addtoeach uses structural recursion to add to each element of the list on which it is called. total1 uses structural recursion to apply addtoeach to lists of length n 1 ,..., 1 , . Intuitively, the way we add up a list by hand doesnt take this long. Structural recursion is doing too much work. CS 135 Fall 2008 10: Accumulators 6 Using an accumulator If we were processing a sublist starting at, say, the fifth item of the list, and we knew the sum of the first four items, we could just add that to the fifth item. In the structurally recursive version, we have forgotten everything about the first four items at this point. The solution is to pass that information down in an accumulator , an additional parameter to the recursive function. The function computes the new value of the accumulator for the recursive application....
View
Full
Document
 Fall '07
 VASIGA

Click to edit the document details