Week 7: Recursions, Recursions, Recursions
After all the labs, lectures, and assignment, I've realized how powerful and efficient recursion really is. It turns what could be tons of redundant lines into less simple and make it easy to understand code. Basically recursion is calling itself again and again until the condition is satisfied. I find that it is very useful when dealing with something that you have to do over and over again but will be very difficult or complicated with iteration loops such as for loops and while loops. Recursion makes it much more simpler than using iteration loops in python. Recursion is useful when dealing with a problem that has a sub-problem which is basically the same problem. Therefore if you didn't write recursion you'd have to write a lot of code that basically does the same thing over and over which could get complicated and easily make you do mistakes. Many examples that were provided to us included linked lists or nested lists that dealt with this problem with a sub problem. Since recursion is basically calling itself almost infinitely, a base case is required where when the conditions(case) is met the recursive call is stopped.
The concept seems very simple and isn't really hard to understand. I mean, you just call the function again and again, right? How hard could that be? Well, I actually found myself struggling at first trying to implement recursive codes. So what I've done was trace recursive codes and figure out the outputs on paper. I really thought that this helped a lot in terms of understanding recursion and ways to implement. We also had a lab where we had to trace recursive functions which I thought was really helpful and helped me solidify my understanding of recursion and implementing recursive functions.
Also, list comprehension which was introduced in the first few weeks of classes came handy in recursion. I found list comprehensions to be a bit difficult to understand at first because it was a very new concept to me and wasn't thought in CSC108, especially when if else statements were thrown in inside the list comprehension then you have to do it recursively which made it even more confusing. At first, what I did in order to understand recursion using list comprehension was actually rewrite the list comprehension into the "long, lengthy way" of making lists and then applying recursion from there and then rewrite back into a list comprehension. After some practice, it helped me to understand recursion with list comprehension much better.
I guess the main point of using recursion is to reduce redundancy in codes when solving problems especially with tree data structures and nested lists or other recursive data structures.
The concept seems very simple and isn't really hard to understand. I mean, you just call the function again and again, right? How hard could that be? Well, I actually found myself struggling at first trying to implement recursive codes. So what I've done was trace recursive codes and figure out the outputs on paper. I really thought that this helped a lot in terms of understanding recursion and ways to implement. We also had a lab where we had to trace recursive functions which I thought was really helpful and helped me solidify my understanding of recursion and implementing recursive functions.
Also, list comprehension which was introduced in the first few weeks of classes came handy in recursion. I found list comprehensions to be a bit difficult to understand at first because it was a very new concept to me and wasn't thought in CSC108, especially when if else statements were thrown in inside the list comprehension then you have to do it recursively which made it even more confusing. At first, what I did in order to understand recursion using list comprehension was actually rewrite the list comprehension into the "long, lengthy way" of making lists and then applying recursion from there and then rewrite back into a list comprehension. After some practice, it helped me to understand recursion with list comprehension much better.
I guess the main point of using recursion is to reduce redundancy in codes when solving problems especially with tree data structures and nested lists or other recursive data structures.