Recursion seems like a big concept for beginners who are learning it -- I remember when I first encountered computer science using recursion I was confused as to how recursion actually ran. Back then, the tracing method I learned was a slightly inefficient one (basically writing out each of the steps and variables on stack by hand, like a human debugger), so it took me longer than I expected to grasp the concept. However, after I understood the gist of the self-calling functions, everything became a lot more clear. Here I will sum up what I have learned.
First of all, the structure of recursion is very important. Professor Heap's (Danny's) slides sum it up very well.
(This can be found in the slides for Feb. 29th)
It is absolutely essential to have a base case. Why? Assume a recursive function with no base case. What would happen? Since the program is recursive, one execution will lead to one or more execution, which will subsequently lead to more executions with no end. If there is no stop to the program, it will run on forever. Unless you purposefully want your program to run forever (which is entirely up to you), I'd suggest putting a stop at the end. It doesn't matter whether the base case be a lower limit (like counting down), or an upper limit (like the number of layers the recursion is into), as long as there is something. And then the expression without recursion. Whether it be an executable line of code, or a return statement, as long as it doesn't generate another loop and jump out of the infinite circle, anything's good.
Of course, next comes the part with recursion. Placing it in the else: statement is definitely a safety measure. Though if the expression without recursion is a return statement, an else is not necessary? The expression that follows is the key part that recurs. This is the part where the function calls itself within itself. If no recursion occurs, then there will be no progress because the function execute once on the outer layer of the recursion. Recursion is the repeated calls from outer to inner to go through every layer, like unstacking a matryoshka, or peeling an onion. The function recurs until arriving at the base case, the stop mentionned above.
The most classic example I have encountered is using recursion to find a certain number in the Fibonacci Sequence. The stopping condition is usually n, the nth element in the sequence, and also the number of times the function executes. The non-recursive expression returns the a_(n-2) + a_(n-1) while the recursive expression calls on the function itself again, passing in the next element in the sequence (i+1, i being the current element), the upper limit (n), and the current a_(i-1) and a_(i-2) + a_(i-1).
Other than lists and trees, a lot of sequencing, like geometric and arithmetic sequences can be modeled by recursion. There are also a lot of other usages, such as solving for factorials, solving games (cue: A2, Tower of Hanoi, etc.), and drawing beautiful fractals.
tl;dr - have a base case to prevent infinite loops, than have recursion where the function calls itself within itself, and it's nice to use recursion to solve nested/layered problems.

No comments:
Post a Comment