This is not my first encounter with recursive calls. My first was through a joke that goes as follows:
Student: "How do I learn recursion?"
Teacher: "You have to know recursion to understand recursion..."
It still makes me chuckle and I may have written it on my most recent midterm where we had to trace recursion as a question.
Tracing recursion isn't as easy as I thought it would've been. I fully understand recursion, but to go through each line of the calls is annoying. However this pedantic approach to tracing recursion definitely helped me get the correct answer. At first glance I thought this particular function returned the length of the largest list in a series of nested lists. However after further examination I found that it summed the lengths of all lists in a set of nested lists.
To conclude: recursion is a very useful tool in the arsenal of a computer programmer. However it has to be treated very carefully. While running a certain recursive program, I forgot to fully define the end case, and inadvertently created an infinite loop, which caused my computer to freeze. So a warning to future me, always read your recursive code carefully before running.
No comments:
Post a Comment