Skip to content

Commit 7256235

Browse files
committed
Applied changes from code review.
1 parent 206a562 commit 7256235

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

concepts/recursion/about.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This is usually managed by updating one or more variable values to progressively
1818
This is an efficient implementation, but it can be somewhat cluttered when looking at the code.
1919

2020
Recursion, rather than updating _variable state_, can pass _updated values_ directly as arguments to the next call (iteration) of the same function.
21-
This declutters the body of the function and can clarify how each update happens.
21+
This de-clutters the body of the function and can clarify how each update happens.
2222
However, it is also a less efficient implementation, as each call to the same function adds another frame to the stack.
2323

2424
## Recursion: Why and Why Not?
@@ -55,7 +55,7 @@ def paydates_for_year(year, weekday, ordinal):
5555
"""Returns a list of the matching weekday dates.
5656
5757
Arguments:
58-
year (int): The year (e.g. 2022)/
58+
year (int): The year (e.g. 2022).
5959
weekday (int): The weekday number (e.g. 3 for Wednesday).
6060
ordinal (int): Which weekday of the month (e.g. 2 for the second day).
6161
@@ -121,13 +121,13 @@ Adya is happy that there are no more nested loops, no mutated state, and 2 fewer
121121

122122
She is a little concerned that the recursive approach uses more steps than the looping approach, and so is less "performant".
123123
But re-writing the problem using recursion has definitely helped her deal with ugly nested looping (_a performance hazard_), extensive state mutation, and confusion around complex conditional logic.
124-
It also feels more "readable" - she is sure that when she comes back to this code after a break, she will be able to read through and remember what it does more easily.
124+
It also feels more "readable" she is sure that when she comes back to this code after a break, she will be able to read through and remember what it does more easily.
125125

126126
In the future, Adya may try to work through problems recursively first.
127127
She may find it easier to initially walk through the problem in clear steps when nesting, mutation, and complexity are minimized.
128128
After working out the basic logic, she can then focus on optimizing her initial recursive steps into a more performant looping approach.
129129

130-
Even later, when she learns about `tuples`, Adya could consider further "optimizing" approaches, such as using a `list comprehension` with `Calendar.itermonthdates`, or memoizing certain values.
130+
Even later, when she learns about [concept:python/tuples](), Adya could consider further "optimizing" approaches, such as using a [`list comprehension`][list-comprehension] with [`Calendar.itermonthdates`][itermonthssates], or [memoizing][memoization] certain values.
131131

132132

133133
## Recursive Variation: The Tail Call
@@ -187,8 +187,8 @@ However, it is always important when using recursion to know that there will not
187187
Some languages are able to optimize tail calls so that each recursive call reuses the [stack frame][stack-frame] of the first call to the function (_similar to the way a loop reuses a frame_), instead of adding an additional frame to the stack.
188188
Python is not one of those languages.
189189
To guard against stack overflow, Python has a recursion limit that defaults to one thousand frames.
190-
A [RecursionError](https://docs.python.org/3.8/library/exceptions.html#RecursionError) exception is raised when the interpreter detects that the recursion limit has been exceeded.
191-
It is possible to use the [sys.setrecursionlimit](https://docs.python.org/3.8/library/sys.html#sys.setrecursionlimit) method to increase the recursion limit, but doing so runs the risk of having a runtime segmentation fault that will crash the program, and possibly the operating system.
190+
A [RecursionError][RecursionError] exception is raised when the interpreter detects that the recursion limit has been exceeded.
191+
It is possible to use the [sys.setrecursionlimit][sys.setrecursionlimit] method to increase the recursion limit, but doing so runs the risk of having a runtime segmentation fault that will crash the program, and possibly the operating system.
192192

193193
## Resources
194194

@@ -197,12 +197,16 @@ To learn more about using recursion in Python you can start with
197197
- [Real Python: python-recursion][Real Python: python-recursion]
198198
- [Real Python: python-thinking-recursively][Real Python: python-thinking-recursively]
199199

200-
[python-programming: recursion]: https://www.programiz.com/python-programming/recursion
200+
201201
[Real Python: python-recursion]: https://realpython.com/python-recursion/
202202
[Real Python: python-thinking-recursively]: https://realpython.com/python-thinking-recursively/
203203
[RecursionError]: https://docs.python.org/3.8/library/exceptions.html#RecursionError
204-
[setrecursionlimit]: https://docs.python.org/3.8/library/sys.html#sys.setrecursionlimit
205-
[divide and conquer]: https://afteracademy.com/blog/divide-and-conquer-approach-in-programming
206204
[cumulative]: https://www.geeksforgeeks.org/sum-of-natural-numbers-using-recursion/
205+
[divide and conquer]: https://afteracademy.com/blog/divide-and-conquer-approach-in-programming
206+
[itermonthssates]: https://docs.python.org/3/library/calendar.html#calendar.Calendar.itermonthdates
207+
[list-comprehension]: https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
208+
[memoization]: https://dbader.org/blog/python-memoization
209+
[python-programming: recursion]: https://www.programiz.com/python-programming/recursion
207210
[stack-frame]: https://shanechang.com/p/python-frames-systems-programming-connection/
211+
[sys.setrecursionlimit]: https://docs.python.org/3.8/library/sys.html#sys.setrecursionlimit
208212
[what-is-the-call-stack]: https://en.wikipedia.org/wiki/Call_stack

0 commit comments

Comments
 (0)