Friday, February 28, 2014

Week 7 – Recursion

Though I've mostly been writing about recursion for my weekly topics (as that is what we've been learning), this post is to be solely dedicated to this very important concept.

Recursion is the concept of calling a function from within itself. Usually we would define a function and then call it after we've completely finished writing its body and output. But in recursion, we define a function and call it within its own body. This is very useful with objects whose structures are defined recursively, and we've seen it used with binary trees, linked lists, nested lists, and other types of the sort.

A binary tree, for example, has an internal node as its root and left and right children, which are also binary trees themselves. So to count the number of nodes in the tree, we would count the number of nodes in the left subtree, then count the number of nodes in the right subtree, then add 1 more node for the internal node at the root. This turns into a function like the following:

def count(n: Tree):
return 1 + sum([count(n) for n in t.children])

We use the count function inside its body because we are also counting the number of nodes in the tree's left and right children. And what are those children? Trees. So it only makes sense that we would use the same function for the same operation. And when there are no children, the recursive calls stop because t.children will have no values and the return value will only be 1. Then the function will begin to wind back up, counting 1 for each leaf and adding up the number of leaves in a child, then adding 1 for that node and combining the value with the value of the other child, and finally adding 1 to account for the root node.

So, as seen, we have no doubt focused a lot on recursion this semester. But what is so important about it? Why is it so useful that we are expected to know how it works and to be able to use it in any applicable situation?

The way I see it, our golden rule is a key reason: laziness. Recursion will usually provide the easiest and shortest solution to a problem.

It's the easiest to write, read, and understand code, which makes it less work for anyone doing anything with the program.

First, as the initial writer of the code, simply calling the function that you just wrote (since you know it does exactly what you want) is much easier than writing nested loops or having counter variables to try and keep track of how deep you are into the structure...and other things like that. In most cases a function involving a recursive structure will be very difficult to write non-recursively. Here, recursion is easier because it will automatically stop as long as you provide the action to perform when the base case is reached. You don't need to know when the base case is reached (and knowing this is hard because there will be many layers/depths, one within another, so you can't really count them), because the function will recognize that.


Afterwards, when the next person has to read your code, they will see the places where you've called the function recursively and think something like, 'Ah, they just did the same operation on the next level of this object.'. In other words, when reaching a level where we already know what happens, we can just stop and substitute the previously-found result. This is also what we do when writing recursive code.

Friday, February 14, 2014

Week 6 – Trees and Finishing Assignment 1

The recursive structure of trees allows their functions to perform the same action on each of their individual nodes and leaves as they do on the whole tree. This makes most functions and methods for trees recursive.

In the example we saw in lecture of the basic tree class, a tree had two attributes: its value and children. The value could be any object, but its children was a list of—well--more trees. This was what allowed the same actions to be done on both the tree and its subtrees, recursively, until it got to the leaves, stopped, and gradually made its way back to the entire tree. For instance, to count the number of nodes a tree had, one would count the initial node at the root of the tree and then add up the number of nodes of each child of the tree. Since each child was also a tree, one could use the same function to count the number of nodes for that child.

Other than binary trees, we looked at a type of unary tree called a linked list. A linked list had a head and a rest, and the rest was another linked list containing its own head and rest, and so forth. This made the linked list a recursive structure as well, and we could perform the same operations on each part of the linked list as the linked list itself.


Assignment 1, which I'd started last week, will be due this week. It took me a long time to understand the GUIController and ConsoleController modules, because I was confused about the differences between the graphics of a cheese/stool and the object cheese/stool. That was before I took the time to read the docstring in GUIViewables, which cleared up that CheeseView actually inherits from the Cheese class and the PlatformView class to represent the cheese, so I only had to actually work with the Cheese class. After that, I progressed fairly quickly with the ConsoleController functions and TOAHModel functions. Finally, for the tour_of_four_stools function in Tour.py, I initially did not use recursion to find the value of i; I had used that the most efficient value of i would always be equal to the floor of n/2, with n as the number of cheeses, and just recalculated this value every time I ran through tour_of_four_stools. However, after learning that we were expected to use a recursive solution to find i, I rewrote a function to find i essentially following the formula given in the handout. Other than this, I found my A1 coding process to be a generally smooth one.

Friday, February 7, 2014

Week 5 – More Recursion, Namespaces, and Assignment 1

This week we continued to learn about recursion. What helped me to understand recursion more easily was learning its structure, which consists of the general case and the base case(s). Writing recursive code for the base case first and then applying the concept to the general case makes the process, which can sometimes get messy, a lot clearer to me. We were also shown many different applications of recursion, such as in nested lists, writing a function that can return permutations, and drawing graphics. The turtle animation was very interesting and really showed me, visually, how powerful recursion can be.

We then looked at an example of namespaces to see the differences between defining variables globally, nonlocally, and locally. We were told that we should not reuse variable names even if they are not in the same scope.


I've also started to think about assignment 1. The code for the move_cheeses function that we were presented with in lecture has proven very helpful to my understanding of the recursive nature of this game. I plan to use the example code as the “base case” of the tour_of_four_stools function (which is one of the later parts of the assignment, so I'm not completely certain how it's going to work just yet, but I think it will help a lot).