Week 7:
Recursion and Linked Structures
Recursion is defined as "defining something in terms of itself". The recursion function calls itself in its function body therefore we don't have to make duplicate codes. For example, if we want to find the number of objects in nested list, we must check each nesting list. Then we can utilize the recursion function. If the element in the list is an object, the function counts it as 1 object. If it is a smaller list in the list, the function calls itself and check the inside of the smaller list. Every recursion function has a base
case, which checks if the function has to call itself in function body,
therefore it automatically solves the problem and we do not have to
check a condition for each sub problems.
In my opinion, the recursion is very useful for programmers because it omits duplicate codes and makes function body shorter. However, the recursion functions sometimes are difficult to read and understand what they do. Tracing the recursion functions precisely is very difficult. In conclusion, if we want to write the codes efficiently, we should write recursion.
0 件のコメント:
コメントを投稿