Lecture 10-Recursion - 1 1 Recursion : If you get the...

Info iconThis preview shows pages 1–2. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 1 Recursion : If you get the point, stop; otherwise, see Recursion. Infinite recursion : See Infinite recursion. Read: pp. 403-408 but SKIP sect. 15.1.2 ProgramLive CD, page 15-3, has interesting recursive methods. Download presented algorithms from the website Recursive defnition : A defnition that is defned in terms oF itselF. Recursive method : a method that calls itselF (directly or indirectly). Recursion is oFten a good alternative to iteration (loops), which we cover later. Recursion is an important programming tool. Purely Functional languages have no loops only recursion. CS1110 30 September. Recursion Prelim I 7:309:00PM Thursday, 7 Oct /** = the number oF es in s */ public String noe(String s) { iF (s.length() == 0) { return 0; } // { s has at least one char } } 2 Called the base case Called the recursive case Express the answer with the same terminology as the specifcation, but on a smaller scale: number oF es in s = (iF s[0] = e then 1 else 0) + number oF es in s[1..] return (s[0] = e ? 1 : 0) + noe(s.substring(1)); Notation: s[i] shorthand For s.charAt[i]. s[i..] shorthand For s.substring(i). s 0 1 s.length() 3 Two issues in coming to grips with recursion 1. How are recursive calls executed? 2. How do we understand a recursive method and how do we create one? We discussed the frst issue earlier. IF you execute a call on a recursive method careFully, using our model oF execution, you will see that it works. Briey, a new Frame is created For each recursive call. We do this in the next lecture. DONT try to understand a recursive method by executing its recursive calls! Use execution only to understand how it works....
View Full Document

This note was uploaded on 11/27/2010 for the course CS 9339 at Cornell University (Engineering School).

Page1 / 2

Lecture 10-Recursion - 1 1 Recursion : If you get the...

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

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