Re: Is this recursion?
- From: Patricia Shanahan <pats@xxxxxxx>
- Date: Wed, 31 Jan 2007 00:48:44 GMT
Marion wrote:
The recursive step is the "reversal(aWord,position-1)" call.
The base case is position equal to 0, which is handled differently from
non-zero. Specifically, the recursive step is not done in the base case.
They have the correct relation to each other, if position is initially
non-negative. The recursive step then uses a function of position,
position-1, that ensures that it must reach the base case, position==0.
Incidentally, you should look at your handling for empty strings.
Patricia
Thanks Patricia and Gordon,
I thought my base case was there but U wasn't confident about my reasoning until you two explained it a bit to me.
Patricia, I learned from Gordon in another program that strings were compared using ".equal" and not "==". I'm afraid I will never forget this even after I am 101 years old . . . with senility. It's been burned into my brain, permanently. :p)
True, but I don't see the relevance, because there were no string
compares in your code.
In the simplified example code I provided, the empty string does not exist since the program initialized before calling the recursive method. I understand that calling a recursive method when a negative number condition exists is not a happy event. In my current code that is not shown here, I do check for the empty string condition before proceeding.
Calling a recursive method with negative input does not have to be a
problem. The key condition is that the recursion process should always
reach a base case in a finite number of steps, when starting from any
permitted input.
Patricia
.
- References:
- Is this recursion?
- From: Marion
- Re: Is this recursion?
- From: Patricia Shanahan
- Re: Is this recursion?
- From: Marion
- Is this recursion?
- Prev by Date: Re: New line in a JTextArea (student learning here)
- Next by Date: Re: New line in a JTextArea (student learning here)
- Previous by thread: Re: Is this recursion?
- Next by thread: Re: Is this recursion?
- Index(es):
Relevant Pages
|