Tuesday, 3 February 2015

Recursion Part 1: Understanding the Logic Behind Recursion and Tracing

Today's question is when recursion can be used. Is this technique applicable for all problems, or is there only a subset of problems in which this idea can be used? Of course we only use it in iterables in all our examples in class... but can we be more general? Also, when is it a good idea to use it out of all the cases where it can be used?

But for now, I want to talk about the logic behind recursion
Ennanarts, jesse, and many other sloggers have shown recursion in a pictorial way. As you go in one step, you find that same thing. This kind of understanding is best exemplified by fractals as described by our course-link.

So, I want to show a slightly different approach: recursive form of mathematical expressions.
A sequence can be written in explicit form:
x(n) = 2^n
or
it could be written in recursive form:
x(n) = 2*x(n-1), x(0) = 1

In recursive form, the next value in the sequence is formulated as a function of the previous value. This means that if we can find that previous value x(n-1), we can find this value x(n). So... you do this again and again starting from n... until you reach a certain point... the base case (or in this case n = 0). But what if I gave you x(2) = 4. What is x(3)? Is it neccessary to unravel and go to x(0)? This leads to a fundamental logic involved in the tracing of recursive functions. After you prove/show the behaviour of a "lower level" in this case n - 1, you can use that result. it's not neccessary to go to x(0).

That's just some rationale about the "simplified" tracing scheme of recursive functions and also about another angle at which recursive functions can be thought about.

Next post, I would like to share some recursive functions from last week's lab and another function structure (or a spin off) that I was thinking about.

PS. type recursion on Google... it's really fun

1 comment:

  1. Great post! And yeah, when you google "recursion" it's pretty funny haha!

    ReplyDelete