Module 9

# Module 9 - Generative recursion Readings 27 28 Some...

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 Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 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 file-processing 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 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 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 ;; euclid-gcd: nat nat → nat ;; computes gcd(n,m) using Euclidean algorithm ( define ( euclid-gcd n m ) ( cond [( = m ) n ] [ else ( euclid-gcd 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 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 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 ( 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 Fal 2008 09: Generative recursion 7 In the case of euclid-gcd , 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.

### Page1 / 24

Module 9 - Generative recursion Readings 27 28 Some...

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

View Full Document
Ask a homework question - tutors are online