Trouble understanding recursion (Python)

  • Context: Python 
  • Thread starter Thread starter mrcleanhands
  • Start date Start date
  • Tags Tags
    Python Recursion
Click For Summary

Discussion Overview

The discussion centers around understanding a recursive function that draws a Koch curve in Python. Participants explore the mechanics of recursion, particularly how the function executes and the flow of control through recursive calls.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the execution flow of the Koch function, questioning whether the code continues executing until the last line after calling itself recursively.
  • Another participant clarifies that when n < 3, the function calls fd() instead of itself, and that m will eventually be less than 3, allowing the recursion to terminate.
  • A later reply emphasizes that the code following the recursive call is not dead code, explaining that the recursion tree will eventually reach its base case and then return to execute the remaining calls.
  • One participant suggests visualizing the execution by drawing a tree structure to better understand the flow of the recursion.
  • Another participant distinguishes between "calling the function recursively" and "jumping back to the top of the function," indicating a misunderstanding in the original question.
  • Suggestions are made to run modified versions of the function with print statements to observe the differences in execution between the recursive and non-recursive implementations.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of recursion and the importance of the base case, but there is some confusion regarding the execution flow and the interpretation of the code structure. The discussion remains somewhat unresolved as participants clarify different aspects of recursion without reaching a consensus on the initial confusion.

Contextual Notes

Some participants note that the behavior of the function depends on the specific implementation of fd() and the nature of the recursive calls, which may not be fully defined in the discussion.

Who May Find This Useful

This discussion may be useful for individuals learning about recursion in programming, particularly in Python, and those interested in graphical representations of recursive algorithms like the Koch curve.

mrcleanhands
I'm trying to understand a function which draws a koch curve using a library which draws (lt = left turn, fd = move forward, t represents an object class).

Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
    lt(t, 60)
    koch(t, m)
    rt(t, 120)
    koch(t, m)
    lt(t, 60)
    koch(t, m)

So let's say I start with n = 60, then m=20 and it executes koch(t, 20), but it will never get below koch(t,m) as if the number is above 3 then it keep returning to execute itself again. This is the way I see the code:
Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
   DEAD CODE...

I'm confused what happens at this point. Does it continue executing the code right until the last line and then executes each koch(t,m) call separately?
 
Technology news on Phys.org
That's what it looks like to me. Have you ran it?
 
If n < 3, then koch() calls fd(), not koch(). Otherwise it sets m = n / 3, and makes calls to koch(m,...) (so m will eventually be < 3).
 
mrcleanhands said:
I'm trying to understand a function which draws a koch curve using a library which draws (lt = left turn, fd = move forward, t represents an object class).

Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
    lt(t, 60)
    koch(t, m)
    rt(t, 120)
    koch(t, m)
    lt(t, 60)
    koch(t, m)

So let's say I start with n = 60, then m=20 and it executes koch(t, 20), but it will never get below koch(t,m) as if the number is above 3 then it keep returning to execute itself again.


This is the way I see the code:
Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
   DEAD CODE...

I'm confused what happens at this point. Does it continue executing the code right until the last line and then executes each koch(t,m) call separately?

If n<3 is your "base case". It is not defined here what fd() does, but the important thing is that the recursion tree for Koch() stops here. During the execution of Koch() n will never grow, so you are guaranteed that the base case will be hit at some point.

The code below koch(t,m) is not dead code! The recursion tree for Koch(t,m) just needs to bottom out and hit its own base case before returning. Once it does the execution continues as expected, with more Koch() calls and more recursions. They will eventually all stop though.

Edit: To help you visualize what happens, draw a tree for the execution of Koch() with relatively simple values.

Let's say we start with n = 18. Your tree will have a top-most node, where n = 18. Below it will be four nodes connected with lines, each with n = 18/3 = 6. Now each of those four nodes will again have four children (so 4*4=16 children at level 2) and n = 2. Now the tree stops, since n < 3.

To follow the program flow, start at the top and trace the tree down. Start with the leftmost child of the root. Since this child has a leftmost child of its own, trace that line down too. Now you should be at level 2 of the tree in the far left part. What happens now is that the program hits its base case, and starts returning again. So you trace up one node, and follow the line of the second child (the right next to the one you visited before).
This continues until all the child nodes have been visited, in which case you trace back up one level and continue from there with the rest of the un-visited child nodes.
 
Last edited:
I think you are confusing "calling the function recursively" with "jumping back to the top of the function". They are not the same thing.

You might want to try running something like this (I'm not a Python user so apologies for any syntax errors!)
Code:
def koch18(t, n):
    m = n/3.0
    koch6(t, m)
    lt(t, 60)
    koch6(t, m)
    rt(t, 120)
    koch6(t, m)
    lt(t, 60)
    koch6(t, m)
    return

def koch6(t, n):
    m = n/3.0
    koch2(t, m)
    lt(t, 60)
    koch2(t, m)
    rt(t, 120)
    koch2(t, m)
    lt(t, 60)
    koch2(t, m)
    return

def koch2(t, n):
   fd(t, n)
   return
That code is not recursive. Put some "print" statements so you can see how works when you do "koch18(t,18)".

That version of the code isn't very clever, because obviously "koch18" and "koch6" are almost exactly the same. The point of the recursive version is that you can replace them (and every other "koch..." function) with just ONE function, which calls itself.

Put some print statements in the original version, and compare what "koch(t, 18)" and "koch18(t,18)" do.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K