Tail Recursion

You cannot view this unit as you're not logged in yet. Go to Account to login.

2 Comments

  1. Sergey Makarichev on December 11, 2022 at 4:35 am

    Hi, Mark!
    1) It’s better to add some comments why the strings below are not 6 various stack calls (the sample in the middle of the lesson):
    StackTest.recurse(0)
    StackTest.recurse(1)
    StackTest.recurse(2)
    StackTest.recurse(3)
    StackTest.recurse(4)
    StackTest.recurse(5)

    For me it looks like 6 independent stack calls – StackTest.recurse() function is called with various arguments 6 times, and all these calls are added into stack.
    For example, in Kotlin tail recursion transforms into plain iteration, but that iteration is within 1 function call. And that’s not what I see in the example above – I expected 1 function call in Elixir too, but I saw 6 function calls…

    2) All this means that Elixir doesn’t have a limited stack (like С or Java), but it’s stack limit equals the total amount of RAM of the system?
    So tail recursion won’t last forever, but until all system RAM is used, right?

    • Mark Ericksen on December 19, 2022 at 7:27 am

      Hi Sergey,

      Elixir also has a RAM limited stack. If the recursive call is followed by more instructions, then a regular stack is used and it can be blown. When the final instruction is a recursive call, then it is “tail recursion” and will effectively run forever without pushing on to the stack.

      Yes, the list of functions could be confusing since the name is “recurse”. I’ll have to think about a clearer way to express that.

Leave a Comment

You must be logged in to post a comment.