Skip to content

Commit 3e78cb0

Browse files
committed
make code in 7.3 more parallel
1 parent af81531 commit 3e78cb0

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

source/ch7_recursion.ptx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public class ArrayProcessor {
256256
public static void main(String[] args) {
257257
int[] numbers = {1, 2, 3, 4, 5};
258258
int result = sumArray(numbers);
259-
System.out.println("The sum of [1, 2, 3, 4, 5] is " + result);
259+
System.out.println("The sum of " + Arrays.toString(numbers) + " is " + result);
260260
}
261261
}
262262
</code>
@@ -274,7 +274,7 @@ public class ArrayProcessor {
274274
<section xml:id="recursion-limits-in-java">
275275
<title>Recursion Limits: Python vs. Java</title>
276276
<p>
277-
The consequence of deep recursion, running out of stack space, is a concept you've already encountered in Python. Java handles this in a very similar way, throwing an error when the call stack depth is exceeded.
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.
278278
</p>
279279
<p>
280280
The key difference is the name of the error:
@@ -284,10 +284,10 @@ public class ArrayProcessor {
284284
<li>In Java, this throws a <c>StackOverflowError</c>.</li>
285285
</ul>
286286
<p>
287-
Neither language supports <idx> tail call optimization </idx><term>tail call optimization</term>, 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 the preferred solution in both Python and Java.
287+
Neither language supports <idx> tail call optimization </idx><term>tail call optimization</term>, 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.
288288
</p>
289289
<p>
290-
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a RecursionError.
290+
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a <c>RecursionError</c>.
291291
</p>
292292
<program xml:id="python-recursion-error" interactive="activecode" language="python">
293293
<code>
@@ -297,32 +297,33 @@ public class ArrayProcessor {
297297
"""
298298
cause_recursion_error()
299299

300-
# Standard Python entry point
301-
if __name__ == "__main__":
302-
print("Calling the recursive function... this will end in an error!")
303-
304-
# This line starts the infinite recursion.
305-
# Python will stop it and raise a RecursionError automatically.
306-
cause_recursion_error()
300+
print("Calling the recursive function... this will end in an error!")
301+
302+
# The line below will start the infinite recursion.
303+
# Python will stop it and raise a RecursionError automatically.
304+
# Each call adds a new layer to the program's call stack.
305+
# Eventually, the call stack runs out of space, causing the error.
306+
cause_recursion_error()
307307
</code>
308308
</program>
309309

310310
<p>
311-
The following Java code demonstrates a similar situation, where a method calls itself indefinitely without a base case, leading to a StackOverflowError.
311+
The following Java code demonstrates a similar situation, where a method calls itself indefinitely without a base case, leading to a <c>StackOverflowError</c>.
312312
</p>
313313
<program xml:id="java-stack-overflow" interactive="activecode" language="java">
314314
<code>
315315
public class Crash {
316316
public static void causeStackOverflow() {
317-
// This method calls itself endlessly without a stopping condition (a base case).
318-
// Each call adds a new layer to the program's call stack.
319-
// Eventually, the stack runs out of space, causing the error.
317+
// The line below will start the infinite recursion.
318+
// Java will stop it and raise a StackOverflowError automatically.
319+
// Each call adds a new layer to the program's call stack.
320+
// Eventually, the call stack runs out of space, causing the error.
320321
causeStackOverflow();
321322
}
322-
// A main method is required to run the program.
323+
// A main method is required to run the Java program.
323324
public static void main(String[] args) {
324325
System.out.println("Calling the recursive method... this will end in an error!");
325-
// This line starts the infinite recursion.
326+
326327
causeStackOverflow();
327328
}
328329
}

0 commit comments

Comments
 (0)