Recursion and mathematical induction are closely related. It was so obvious that I cannot believe I didn't see it before. Both of them require you to find the 'base case', the simplest problem that can be solved, first. After that, you find the more complex problems can simply be broken up into simpler problems that can be solved using the base case. Also, once we know how to figure out the previous case (in induction, we assume that to be the case with k), then the next case's answer is the previous case's answer plus some version of k+1. This connection helped me understand proof by induction in my calculus class.
The concept of recursion is easy to understand for me, but interpreting it into code is another story. Usually, the recursive code we work with is not very long and contains a few list comprehensions. I am not that used to using them yet, so it can be tricky to figure out exactly what kind of comprehensions I need for the statement to make sense. Usually the code ends up too complicated. The numerous brackets do not aid in relieving me of my confusion either. One approach that I found really useful was to write recursive code "the long way", as in using if statements and for loops that take up quite a few lines. I do that first and test to see if the program works, and then I try to translate that into the shorter version that we are shown in examples in lecture, and then see if it works the same as before. This method seems to have improved my ability to analyze and write recursive functions for assignment 2. However, I think there are some instances when I do have to write a recursive function "the long way". A part of assignment 1 required us to write a few recursive functions that needed more than one line of recursive code.
No comments:
Post a Comment