10-genacc-post3up

10-genacc-post3up - Generative recursion Readings 27 28 •...

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: 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 non-recursive 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 domain-specific knowledge. CS 135 Fall 2009 10: Generative recursion 2 Example revisited: GCD ;; euclid-gcd: nat[ > 0] nat[ > 0] → nat ;; computes gcd(n,m) using Euclidean algorithm ( define ( euclid-gcd n m ) ( cond [( zero? ( remainder n m )) m ] [ else ( euclid-gcd 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 non-recursive 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 ( sum-list ( list 3 6 5 4 )) ⇒ ( + 3 ( sum-list ( list 6 5 4 ))) ⇒ ( + 3 ( + 6 ( sum-list ( 5 4 )))) ⇒ ... The depth of recursion of any application of sum-list 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 euclid-gcd , 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 euclid-gcd 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.

Page1 / 37

10-genacc-post3up - Generative recursion Readings 27 28 •...

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