2014年3月2日 星期日

Recursion: An Useful/Helpful Coding Technique

    Recursion has been my favorite coding technique since I learned it recently. I found out that it is very useful when I was writing assignment one: Hanoi Tower. Using recursion saves us a lot of time and the code will be a lot shorter since it keeps on calling the function itself until it meets the base case, particularly, when we're dealing with a huge and complicated algorithm. To know how to write a recursion, we have to find the base case first since it is the trigger of stopping a recursion, else the recursion will never end. It is also important for understanding the recursion we are writing. Then we think about the next recursive step in order  to complete the recursion.

    Writing recursion was hard for me at first. The idea of calling the function itself without actually doing anything confused me a lot. Later, I figured out that I was confused since I was not considering about the base case. List comprehension is a lovely recursive technique. I used a lot of recursions when I am coding. Especially, for the tree methods, list comprehension makes life a lot easier for us to understand how the methods were written. Recently, when I am dealing a list of lots of inner lists, I always use list comprehension to sort the list first, then I do whatever I need to meet the function.
   

沒有留言:

張貼留言