Half way through the term and we are exposed to more and more recursive structure design in the course. Recursion is the process of repeating items in a self-similar way. A recursive function that we are talking about in python, is a function that executes the function itself repeatedly until a certain constrain is met. We have seen a few recursive structures so far such as 'stacks', 'nested list', 'linked list' and 'tree, those we usually called ADTs. Recursion is used widely in all these abstracted data types to get access from them.
The most obvious reason why recursive function works in such a data type like 'nested list' is that the list itself contains more lists, and each of them may contain more and so on. We can always use for loops and if statements to write functions on them, nevertheless, the use of recursion would be more wise. By using the recursive structure, all we need to consider is that the most simple senior and let the recursive function handle the rest by itself. When you using the recursion, not only your code becomes much clearer and simpler, but also it makes you looking like a pro!
No comments:
Post a Comment