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: Generative recursion Readings: Sections 25, 26, 27, 28 • Some subsections not explicitly covered in lecture • Section 27.2 technique applied to strings CS 135 Fall 2009 10: Generative recursion 1 What is generative recursion? Structural recursion, which we have been using so far, is a way of deriving code whose form parallels a data definition. Generative recursion is more general: the recursive cases are generated based on the problem to be solved. The nonrecursive cases also do not follow from a data definition. It is much harder to come up with such solutions to problems. It often requires deeper analysis and domainspecific knowledge. CS 135 Fall 2009 10: Generative recursion 2 Example revisited: GCD ;; euclidgcd: nat[ > 0] nat[ > 0] → nat ;; computes gcd(n,m) using Euclidean algorithm ( define ( euclidgcd n m ) ( cond [( zero? ( remainder n m )) m ] [ else ( euclidgcd m ( remainder n m ))])) CS 135 Fall 2009 10: Generative recursion 3 Why does this work? Correctness: Follows from Math 135 proof of the identity. Termination: An application terminates if it can be reduced to a value in finite time. All of our functions so far have terminated. But why? For a nonrecursive function, it is easy to argue that it terminates, assuming all applications inside it do. It is not clear what to do for recursive functions. CS 135 Fall 2009 10: Generative recursion 4 Termination of recursive functions Why did our structurally recursive functions terminate? A structurally recursive function always makes recursive applications on smaller instances, whose size is bounded below by the base case (e.g. the empty list). We can thus bound the depth of recursion (the number of applications of the function before arriving at a base case). As a result, the evaluation cannot go on forever. CS 135 Fall 2009 10: Generative recursion 5 ( sumlist ( list 3 6 5 4 )) ⇒ ( + 3 ( sumlist ( list 6 5 4 ))) ⇒ ( + 3 ( + 6 ( sumlist ( 5 4 )))) ⇒ ... The depth of recursion of any application of sumlist is equal to the length of the list to which it is applied. For generatively recursive functions, we need to make a similar argument. CS 135 Fall 2009 10: Generative recursion 6 In the case of euclidgcd , our measure of progress is the size of the second argument. If the first argument is smaller than the second argument, the first recursive application switches them. After that, the second argument is always smaller than the first argument in any recursive application. CS 135 Fall 2009 10: Generative recursion 7 The second argument always gets smaller in the recursive application (since m > n mod m ), but it is bounded below by 0. Thus any application of euclidgcd has a depth of recursion bounded by the second argument....
View
Full
Document
This note was uploaded on 02/12/2010 for the course CS 135 taught by Professor Vasiga during the Fall '07 term at Waterloo.
 Fall '07
 VASIGA
 Recursion

Click to edit the document details