Thursday, 3 March 2016

Week 7 -- def recursion(value):

In this week's post, I'll be giving some insight into recursion, including how it works, initial thoughts and difficulties, and how to build a recursive function from the knowledge that we currently have.

First of all, the principal characteristic of recursion and a recursive function is that the function will at some point call itself in the body. This can and has been tricky to understand at first, especially the early stages of tracing. Now, generally speaking from what we've seen so far, there is usually one "base" case in which recursion isn't necessary, this can be useful and helpful when tracing through an example call. To give an example from the lectures, imagine a function which aims to determine the depth of a list and takes an object as a parameter - this may or may not be a list; the base case would be if the given argument is not a list, in which case a depth of 0 would be returned; however, if the argument is in fact a list, one could use recursion to go through this list (assuming it is finitely nested), calling the function on every object in the list, until recursion is no longer necessary and the required output would be given.

One of the principal concepts that we were made to understand while tracing through a recursive function, is to remember that it is not necessary to expand for arguments with examples with characteristics that you have seen before. To go back to the example of the previous paragraph, while tracing through a list of depth 2, it would not be necessary to expand any time you encounter an inner list of depth 1, given that you would already know how to do such a thing - otherwise the tracing process becomes extremely tedious and can result in practically unreadable text.

Now on to a brief explanation of the way we've structured our recursive functions to this point: it is perhaps important to note that we have mostly been working with list objects. Like before, there is generally a base case in which recursion isn't necessary; in our list examples, this has usually occurred when the given argument is not a list, and something is returned - this is done by checking with an if condition. Then, there is another condition, indicating that we in fact have a list that we must go through. Generally speaking, we make use of Python built-in functions such as sum, max, any, etc., then these functions are used on a list that we create through list comprehension, which uses recursion as it checks through every item in the list. Obviously, this is the general case up until now and not all functions will follow this structure, but it gives us a good idea about how such functions work, and their use compared to similar functions which may use a for loop or other methods to do the same thing.

As we learn more about recursion, I hope to get more comfortable in creating functions which require it, and also understanding how to efficiently trace through a recursive function call.

No comments:

Post a Comment