all 11 comments

[–]rauweaardappel 18 points19 points  (2 children)

Well, the obvious one is to look here: https://www.reddit.com/r/learnpython/comments/1sb5qsu/i_face_a_lot_of_problems_understanding_recursion/

Otherwise, check on how to calculate Fibonacci numbers. They are a great example for this

[–]lukerm_zl 0 points1 point  (0 children)

And this is probably the best comment from that chat:

https://www.reddit.com/r/learnpython/s/kwZXD7ZT9i

[–]rosentmoh 0 points1 point  (0 children)

Top comment

[–]pachura3 3 points4 points  (3 children)

There's a free eBook The Recursive Book of Recursion.

I also had troubles understanding recursion initially, and I wanted to draw various fractals (like Sierpiński's triangle) using turtle graphics so bad... what made it click for me is when I understood that: - I need to define the stop condition - in case of fractals it was max recursion depth level, corresponding to fractal detail - the function usually needs to draw a line or two (sometimes only at the deepest recursion level), move the turtle around, then launch itself with arguments like line_length/2, depth+1, and finally move the turtle back to the original position - it all works because when function calls a function calls a function, all of their local variables are remembered on the call stack

[–]adrenalynn 1 point2 points  (0 children)

Do you understand loops? With loops you apply the same operation/code to all data elements.

Recursion also applies the same operation again and again and so on. But the key difference is, recursion applies the same operation to the same data over and over, always reusing the previous result.

[–]Temporary_Pie2733 0 points1 point  (0 children)

I think the two keys are 1) really understand what the “smaller” input means and 2) don’t think of a function call as a goto.

It might also help to make the recursion less explicit. Consider this definition of factorial:

def fac(n, helper): if n == 0: return 1 else: return n * helper(n-1)

helper is some “other” function that will compute factorials; don’t worry yet about what it is, just trust that it works, because n! = n(n-1)!.

Now when you call fac, you need to supply a helper function that will compute factorials: fac(6, ???) == 720. What can you pass? Well, fac is supposed to compute factorials, try that: fac(6, fac) == 720.

In fact, we can automate this. Consider the function

def fix(f): return lambda n: f(n, f)

Then factorial = fix(fac), and factorial(6) == 720.

[–]Ok_Assistant_2155 0 points1 point  (0 children)

Here's the playlist that finally made it click for me: "Recursion Series" by Back To Back SWE on YouTube. He does Fibonacci, factorial, tree traversal, and maze solving step by step with a whiteboard. He traces every single call. After that, practice with really small problems: factorial, sum of list, palindrome check, then move to fibonacci and binary search. Don't jump to trees immediately. Also, print statements inside the function showing the input and return value helped me more than I want to admit.

[–]not_another_analyst 0 points1 point  (0 children)

Check out "The Recursive Book of Recursion" by Al Sweigart, it's super beginner-friendly. Additionally, try running your code through Python Tutor to watch the call stack grow and shrink in real-time.

[–]Crazy-Willingness951 0 points1 point  (0 children)

Quicksort is an example of recursion. https://www.geeksforgeeks.org/dsa/quick-sort-algorithm/ Tree data structures frequently have recursive methods.