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 will not be explicitly covered in lecture, but serve as a good source of examples, notably the topics in 27.3 and 27.4 which are also covered in Math 137 labs. Instead of covering the fileprocessing example of 27.2 in class, we will use essentially the same technique on strings. You should understand both situations. CS 135 Fal 2008 09: 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 Fal 2008 09: Generative recursion 2 Example revisited: GCD In module 06, we briefly discussed this identity: gcd ( n,m ) = gcd ( m,n mod m ) If n < m , this identity just switches m and n , so we don’t have to do anything special to handle the case that m > n . CS 135 Fal 2008 09: Generative recursion 3 ;; euclidgcd: nat nat → nat ;; computes gcd(n,m) using Euclidean algorithm ( define ( euclidgcd n m ) ( cond [( = m ) n ] [ else ( euclidgcd m ( remainder n m ))])) CS 135 Fal 2008 09: Generative recursion 4 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 Fal 2008 09: Generative recursion 5 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 Fal 2008 09: Generative recursion 6 ( 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 Fal 2008 09: Generative recursion 7 In the case of euclidgcd , our measure of progress is the size of the second argument....
View
Full
Document
This note was uploaded on 10/21/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