Author Topic: Issues with understanding recursion  (Read 1239 times)

0 Members and 1 Guest are viewing this topic.

Offline fuicious

  • Serf
  • *
  • Posts: 47
  • Cookies: 0
    • View Profile
Issues with understanding recursion
« on: January 08, 2016, 10:58:42 pm »
Okay as the title suggests, I'm having some problems getting my head around recursion. I've been over a couple of tuts, articles and tasks that involve recursion but the explanations are kinda alike and don't help me very much. And for the tasks I've solved using recursion I would have never gotten the idea to use it if it wasn't suggested as a solution to the problem.

I've searched for it on the forum but there's only a general explanation of what a recursion is and I do understand that. I'm sorry if there is something I didn't find.

So what I'm asking is if there's any resource you guys would recommend that helped you really understand recursion. Does it come naturally to you to approach a certain problem recursively? Because I don't really see a point in using just for the sake of doing so. So if anyone got some tips, they would be more than appreciated.
« Last Edit: January 08, 2016, 10:59:10 pm by fuicious »

Offline Phage

  • VIP
  • Overlord
  • *
  • Posts: 1280
  • Cookies: 120
    • View Profile
Re: Issues with understanding recursion
« Reply #1 on: January 08, 2016, 11:33:27 pm »
My simple reply: http://stackoverflow.com/questions/717725/understanding-recursion

Get on IRC if you want an in-depth talk about it.
"Ruby devs do, in fact, get all the girls. No girl wants a python, but EVERY girl wants rubies" - connection

"It always takes longer than you expect, even when you take into account Hofstadter’s Law."

Offline vinci

  • NULL
  • Posts: 4
  • Cookies: 0
    • View Profile
Re: Issues with understanding recursion
« Reply #2 on: January 08, 2016, 11:55:23 pm »
Recursion is for repetitive tasks, or functions that call themselves. Imagine two mirrors facing each other. That's recursion. Unless you have some condition to break the loop it is infinite. You should look up factorials. That is one application of recursive calls

Offline Trevor

  • Serf
  • *
  • Posts: 39
  • Cookies: 18
  • Coder, Reverser
    • View Profile
Re: Issues with understanding recursion
« Reply #3 on: January 09, 2016, 08:20:18 am »
So what is recursion ?

When a function calls itself, it is called a recursive function. Some problems by nature are recursive.
 
Lets take the case of Fibonacci numbers, it's a series of where every number is the sum of the preceding two numbers. The first and second numbers of the series are 0 and 1 respectively by definition since they do not have two preceding numbers.

Thus the series looks like:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

We can define the nth term of the fibonacci series by,
t0 = 0
t1 = 1
tn = tn-1 + tn-2

Now suppose we need to calculate the 15th term i.e. t15 of the series.
How would you go about ?

Now,
t15 = t14 + t13

To calculate t15 we need the values of t14 and t13.
Similarly, to calculate t13 we need the values of t12 and t11

Hence, you can see each term of the fibonacci series is defined by the preceding two fibonacci numbers; and again each of the two preceding numbers is defined by their respective predecessors.

This is a called recursive definition.

In a hypothetical language, we can write a function to calculate the nth of the series as:
Code: [Select]
function Fibonacci(n: Integer)
    if (n = 0) return 0  //first term is zero
    else if (n = 1) return 1 //Second term is 1
    else return Fibonacci(n-1) + Fibonacci(n-2)
end

The first two if statements are called base cases. Without them the function would call itself endlessly. In every recursive problem we need base cases to prevent an infinite loop.

Hope this explanation clears thing up a bit :)

Offline fuicious

  • Serf
  • *
  • Posts: 47
  • Cookies: 0
    • View Profile
Re: Issues with understanding recursion
« Reply #4 on: January 09, 2016, 12:37:52 pm »
@vinci and @Trevor,
I appreciate your answer, but as I've already mentioned I do understand what recursion is and I can manage those simple examples you mentioned.
However, the problem is that I would always choose to do this iteratively instead of recursively, but I do admit that recursion here looks much better in my opinion, that's why I'm trying to understand it.
Yesterday I wanted to write a function that prints out permutations of string. Didn't really have much success so I looked it up on the internet and there I had learned an algorithm that uses recursion and in the end I managed to write it in code. But not in a million years would it have occurred to me to try to approach that particular problem recursively...

@Phage, thanks for your input, I'll check it out. I don't really wanna bother you on IRC, reading material is enough I think.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Issues with understanding recursion
« Reply #5 on: January 09, 2016, 01:10:38 pm »
Maybe your instructor isn't teaching it a way you understand.

Here is a lecture from MIT that might help.

http://video.mit.edu/watch/recursion-11036/
>>>import this
-----------------------------

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: Issues with understanding recursion
« Reply #6 on: January 09, 2016, 01:49:03 pm »
@vinci and @Trevor,
I appreciate your answer, but as I've already mentioned I do understand what recursion is and I can manage those simple examples you mentioned.
However, the problem is that I would always choose to do this iteratively instead of recursively, but I do admit that recursion here looks much better in my opinion, that's why I'm trying to understand it.
Yesterday I wanted to write a function that prints out permutations of string. Didn't really have much success so I looked it up on the internet and there I had learned an algorithm that uses recursion and in the end I managed to write it in code. But not in a million years would it have occurred to me to try to approach that particular problem recursively...

@Phage, thanks for your input, I'll check it out. I don't really wanna bother you on IRC, reading material is enough I think.
As a functional programmer, I deal a lot more with recursion, so I guess I might add my grain of salt here.
The basic concept behnd recutsion is, as already pointed out, dividing up your problem in small, simple-to-reason-about parts. In principle, every loop you write can be transformed in a tail-recursive function (which is just a function that performs some work and recurses afterwards and obv. has a base case which terminates recursion). However, in imperative languages, you'd rather not do that, because calling a function involves allocating space on the stack, which can in turn lead to an overflow. However, in mathermatics, as well as functional programming, recursion is much more common. This is due to a multitude of factors, but I believe there are two main reasons: First of all, many definitions of "natural structures" (real world objects, number series, ...) are recutsive by definition, because it is a much more concise and short form of describing a problem, thus easier to reason about (in theory). Secondly, you can implement recursion (and function calls) in a different manner without relying to heavily on the stack (which is what compilers for functional languages do), but that's another topic.

So to conclude, recursion and iteration are concepts that are practically interchangeable, where recursion is just more declarative and powerful (you can branch using recursion in ways that a loop doesn't permit), and iteration is more oriented towards an algorithmic thinking, whereas recursion is a more general, versatile and less constrainted approach, which leads to disadvantages: being harder to grasp to a novice and being harder to implement efficiently.

I hope that cleared up your question.
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline fuicious

  • Serf
  • *
  • Posts: 47
  • Cookies: 0
    • View Profile
Re: Issues with understanding recursion
« Reply #7 on: January 09, 2016, 05:41:54 pm »
Maybe your instructor isn't teaching it a way you understand.

Here is a lecture from MIT that might help.

http://video.mit.edu/watch/recursion-11036/
A nice lecture, thank you.

@TheWormKill, I think it's a little clearer to me now. I just need more practice I guess.

Offline iikibT

  • Serf
  • *
  • Posts: 41
  • Cookies: 7
    • View Profile
Re: Issues with understanding recursion
« Reply #8 on: January 09, 2016, 06:47:25 pm »
To learn more about recursion, see this post.
« Last Edit: January 09, 2016, 06:47:45 pm by iikibT »
Hacking for no fun and no profit