Re: Is this recursion?



Marion wrote:
Hello all,

This is the ole reverse a string classroom assignment.

Recursion is soemthing I never quite understood till now but as I perceive it, for a method to be considered recursive, it MUST have a base case and a recursive step.

Fine, with this in mind, I wrote a simple program that reverses a word that doesn't have a base step but it calls itself. Would this be considered by the formal defintion of recursion?

To me, I see the base step as implied by doing nothing while you decrease the parameter by one when you call the method again and THEN you stop calling it recusively when you get to the last element of the string. For example, consider the code:

public class StringReversal {
public static void main(String[] args) {

String aWord = "hello";
System.out.print ("\nReversing " + aWord + " to ");

reversal(aWord,aWord.length()-1);

} // end main

/**reverses a string of characters
* @param string is the string passed */
public static void reversal(String aWord,int position){

System.out.print(aWord.substring(position,position+1));

if (position != 0) {
reversal(aWord,position-1);
}
} // end Reversal method
}

I have a feeling the professor is probably not going to like this because it doesn't fit the 'model' recursion defintion...no base case and recursive step defined. I'm just wondering what other ppl think. It's clear to me and it works! :p

Any thoughts appreciated.


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.

Perhaps you are confused by having some code that is common to the base
case and the recursive case? That happens quite often. For example, a
depth-first tree scans do recursive calls for non-leaf nodes, followed
by processing for the current node that is done regardless of whether it
is a leaf.

Patricia
.



Relevant Pages

  • Re: Root Program cannot print
    ... zero using recursion ... 'public static void main' method. ... The main method must be static and must have a single 'String ' ...
    (comp.lang.java.programmer)
  • Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
    ... that deliberately went one beyond the end of the string, ... Within its framework, which sucks, it is a correct program. ... To do this you changed a value parameter to a reference parameter and ... this makes the recursion go wrong. ...
    (comp.programming)
  • Re: Recursion
    ... and output them in reverse order onto another file. ... The point of recursion is that a method calls itself. ... public void reverse(BufferedReader in, BufferedWriter out) ... Your thought to use String is entirely valid. ...
    (comp.lang.java.help)
  • Re: Recursive functions
    ... recursion is certainly a natural way of defining the ... it IS totally inappropriate to compute ... not mean "inefficient in a typical C implementation". ... If I had an actual requirement to print the string ...
    (comp.lang.c)
  • Re: Three Kinds of Logical Trees
    ... > I think this is just a representation issue. ... > I do have a number, but I am only ttreating as a string. ... I was attempting to isolate the question of tree structure; ... it's not much of an issue; no one uses recursion much. ...
    (comp.databases.theory)