Since I wasn't expecting to have to write an assigned entry on recursion, the last post was a bit early. This post is the actual post regarding recursion, with formal descriptions on the concept and such.
In programming, recursive coding is simply the act of coding a method that solves a problem by solving smaller cases of the same problem from within. In terms of how one programs this, it's usually in the format of the return statement of a function calling the method itself again. For example, a method that is supposed to count the amount of nested elements would begin by counting all the elements of the base list, and then for every one of those elements of that list that are also lists, the function would call itself on the sub-list. A function like this could account for any number of nested items, since the same function of checking a list for sub-lists would occur each time until no sub-lists remained.
Recusion is an ideal method of solving a problem in computer science when the problem at hand is made up of similar, smaller problems. In this case, it is extremely useful; in terms of lines of code it is typically far simpler than writing a series of loops, since recursion will account for any number of nested sub-problems. However, since recursion requires the sub-problems to be similar to that of the base problems, it doesn't necessarily apply to all programming tasks. Recursion is the ideal solution to anything that is nested; for example: nested list, nested objects, trees, etc..
No comments:
Post a Comment