Module 10

Module 10 - Accumulators Readings: Sections 30, 31, 32....

Info iconThis preview shows pages 1–4. 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

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: 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 in-depth 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 ) ( add-to-each ( first alon ) ( total1 ( rest alon ))))])) ;; add-to-each: num (listof num) (listof num) ( define ( add-to-each m alon ) ( cond [( empty? alon ) empty ] [ else ( cons ( + m ( first alon )) ( add-to-each m ( rest alon )))])) CS 135 Fall 2008 10: Accumulators 5 Analysis of total1 add-to-each uses structural recursion to add to each element of the list on which it is called. total1 uses structural recursion to apply add-to-each 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

Page1 / 31

Module 10 - Accumulators Readings: Sections 30, 31, 32....

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

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