Ad
  • Custom User Avatar

    Unfortunately, this is not tail-recursive. All of the intermediate calls need to be kept on the stack because they still have work to do, specifically: adding 2 to the result of the recursive call they are waiting on.

  • Custom User Avatar

    Clever. Not readable. Would not call this "best practice."

  • Custom User Avatar

    How does the solution have to call a more complicated function? sum() is not more complicated than your solution--it eventually resolves to a while loop that simply loops over the iterable, accumulating the sum along the way, and then returns it. Your solution, on the other hand, recursively calls itself n-1 times, setting up a new stack frame each time, and consuming n frames on the stack, which is resource-intensive in both time and space. Your solution isn't even tail recursive, so tail call optimization cannot be used to reduce these costs.

  • Custom User Avatar

    not seem to be a good answer ,(although short)as it has to recall a more complicat function which takes more resource

  • Custom User Avatar

    I definitely didn't need a helper here. Feeling rusty.