We've been talking about Recursion since the beginning, and I have some experience with this programming technique via doing labs and assignments by now, thus I'm getting deeper insight of it. Generally, Recursion is a technique that can define something in terms of itself, which means that functions can call themselves to solve smaller sub-problems.
In computer science, there are many cases that are naturally defined recursively or inductively, for that Recursion can be very helpful. For example, we need to define the maximum number in a possibly nested list of numbers(which is a list whose elements are either numbers or nested number lists) with the function name rec_max(nested numer list), then first need to create a empty list new_list; Secondly we'll look at every element in the list, if the element is a number then append it to new_list, if it is a list then need to call the function rec_max(element); After all, we use the built-in function max(new_list) to get the maximum number. From the above example, the smaller sub-problem is the element that is also a list, and the base case which does not lead to recursive call is the element that is a number. I know how important the base case is, because without it, we'll have infinite recursion and the code will not work.
As Recursion provides a powerful way to describe recursive data structures that are partially composed of smaller and simpler instances of themselves, such as nested number lists, trees, which helps to solve quite a lot of programming problems, therefore it is one of the central ideas of computer science. And I'm sure that to learn computer science well, I have to learn Recursion well first.
No comments:
Post a Comment