Skip to content

Commit 434cd1b

Browse files
committed
put tail-call optimization into an aside
1 parent 5d46240 commit 434cd1b

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

source/ch7_recursion.ptx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,23 +273,30 @@ public class ArrayProcessor {
273273

274274
<section xml:id="recursion-limits-in-java">
275275
<title>Recursion Limits: Python vs. Java</title>
276-
<p>
277-
The consequence of deep recursion, running out of stack space, is a concept you may have already encountered in Python. Java handles this in a very similar way to Python, throwing an error when the call stack depth is exceeded.
276+
<p><idx>recursion limits</idx><idx>call stack</idx>
277+
When using recursion, both Python and Java have practical limits on how deep the recursion can go before running into errors. This is due to the way both languages manage something called the <term>call stack</term>, which is a limited amount of memory used to keep track of function or method calls.
278278
</p>
279279
<p>
280-
The key difference is the name of the error:
281-
</p>
280+
The consequence of running out of call stack space, is a concept you may have already encountered in Python. Java handles this in a very similar way to Python, both throwing an error when the call stack depth is exceeded.
281+
<idx><c>RecursionError</c></idx><idx><c>StackOverflowError</c></idx>
282+
The only difference is the name of the error:
283+
282284
<ul>
283-
<li>In Python, this raises a <c>RecursionError</c>.</li>
284-
<li>In Java, this throws a <c>StackOverflowError</c>.</li>
285+
<li>In Python, overflowing the call stack raises a <c>RecursionError</c> error.</li>
286+
<li>In Java, it throws a <c>StackOverflowError</c>.</li>
285287
</ul>
288+
</p>
289+
<aside>
290+
<p>
291+
<idx>tail-call optimization</idx><idx>TCO </idx><term>Tail-call optimization (TCO)</term> is a technique used in programming to improve the efficiency of recursive function or method calls when the recursive call is the very last operation. This helps prevent some stack overflow errors, especially in languages that support automatic tail call optimization. Unfortunately, neither Python nor Java support automatic tail-call optimization.</p>
292+
</aside>
286293
<p>
287-
Neither language supports tail call optimization, so the practical limits on recursion depth are a factor in both. If an algorithm requires thousands of recursive calls, an iterative, loop-based, approach is likely going to be the preferred solution in both Python and Java.
294+
In both languages, if you write a recursive function that doesn't have a base case or that just recurses too deeply, you'll eventually hit this limit. When this happens, Python will raise a <c>RecursionError</c>, while Java will throw a <c>StackOverflowError</c>. This is because both languages use a call stack to keep track of function calls, and when the stack runs out of space, it results in an error.
295+
Hence, when an algorithm might require thousands of recursive calls, an iterative, loop-based, approach is likely going to be the preferred solution in both Python and Java.
288296
</p>
289-
<p><idx> Tail Call Optimization </idx> <term>Tail Call Optimization</term> (TCO) is a technique used in programming to improve the efficiency of recursive function calls by reusing the current function's stack frame instead of creating a new one. This helps prevent stack overflow errors and reduces memory usage, especially in languages that support it.</p>
290297

291298
<p>
292-
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a <c>RecursionError</c>.
299+
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a <c>RecursionError</c> due to overflowing the call stack.
293300
</p>
294301
<program xml:id="python-recursion-error" interactive="activecode" language="python">
295302
<code>

0 commit comments

Comments
 (0)