The first time I was introduced to anything recursive was during elementary school, when learning about the Fibonacci sequence. I was taught that the rule of this sequence was that the next number was found by finding the sum of the two numbers before it. Therefore, the result was always based on the previous result.
When learning about recursion in Computer Science, it seemed simple enough to understand mentally, but when it came to coding recursive functions, I could not wrap my head around it. I spent some time reading about recursion and looking at examples of recursive code, but I could not reproduce any myself. This was obviously going to be very detrimental to me, because I knew most of the code I would be looking at in the future would require me to know, understand, and implement recursion in order to write more effective code.
A solution I had for my struggle in understanding recursion was asking my friends and peers to tell me the simplest way they understood it. The best example I received was in the form of a present:
"There's this huge gift box, and inside it, there is another gift box, and inside it there is another gift box, and inside it, there is another gift box, etc. In order to get to the present, I'd have to open the first box, then open the next box, and then open the next box, etc.
To do this with a function, you would have the first gift box be your argument. You would check first if when you open the box, there isn't a box inside, because if it isn't a box, it would be your present. However, if there is a box inside, then you would call your function again, this time with the new box being the argument, and the same process would happen all over again, until you reach your present.
The action taken next would be based on the result of the previous action. Obviously if there was no next box inside, and you had reached your present, you would not try to open a box. The box wouldn't even exist for you to open it! In the same way, if you opened the box and there was another box inside, you wouldn't just stop and accept that this box is your true present."
This example really made me happy, not just because it involved a present, but because it took something I thought was complicated, and transformed it into a real life situation. I think it's important that I learned how to relate the troubles I have with functions to real life, because sometimes while searching for answers online, I find that they're convoluted in even bigger problems that I have not had experience with yet. This only serves to make me frustrated and disappointed in myself, taking away from the fun experience of Computer Science.
No comments:
Post a Comment