Sunday, February 2, 2014

The Topic of this Post is "The Topic of this Post is 'The Topic of this Post is "'The Topic of this Post is...

Recursion may not be this week's official topic of choice, but it's a topic of discussion nonetheless, and a very intriguing one at that.

Recursion as a concept is not completely novel: I am also encountering it in Linguistics, which provides a sort of "compass" to how I should navigate the notions (or n-oceans) of recursion.

In a more CS-way, however, recursion is another example of obviously helpful computer logic. To go back to linguistics, recursion occurs when you embed a clause inside another ("when you embed a clause inside another" is an example of language recursion). Recursive humour occurs in the post title, or when I tell you that "to understand recursion, you must understand recursion."

If I want to write a program to track recursion in a string, I could write sentences into a list. For instance:

"Alice looked out from the window, where she watched the birds that ate worms in the yard, and waited for the mailman who came down the road each morning when it wasn't raining."

If I make lists within lists to show the embedded clauses:

['Alice looked out from the window,', ['where she watched the birds', ['that ate worms in the yard,']] 'and waited for the mailman', ['who came down the road each morning', ['when it wasn't raining.']]]

A program which did this would obviously need to be pretty complex. In the example, we can see that we would need it to end the current clause each time a preposition ("where", "that", "who", "when") appears, find some way to distinguish conjunctions ("and") as linking to a different clause, and ending a clause when punctuation or a preposition or a conjunction (sometimes) is encountered. Pretty complicated, but if we say that we can divide it up properly already, then all that is needed is for the program to check if there is a preposition introducing a clause, in which case, it calls itself recursively a creates a smaller list within the list. Otherwise, it encloses the current clause and moves on.

Of course, we discussed other uses for recursion in class that were more simple: using it to create "treebursts" with the Turtle program, or removing 3s from a nested list.
The treebursts were amusing to fiddle with: changing the level caused the recursion to go deeper, and at one point I was able to make the function create a large hexagon made up of smaller hexagons and triangles by fiddling with the number and length of the branches (although colour had to be disabled as there were only three colours given originally). The program was nonetheless very good at literally illustrating the use of recursion, which I now can see coming in handy in the looming assignment 1 (we discussed the formula briefly in class, which only served at first to bring back dark memories of examining the continuity of graphs in math class).
Removing 3s was a simple example as well, but still powerful, although the main thing I took away was the use of "continue" to keep the while loop moving.

The course feels more organized all the same. I still occasionally feel a bit daunted when staring at a new exercise, but the lab-work is always a great aid in providing relief in that regard. I couldn't help but feel a sense of achievement when the class Vehicle code we worked on in our lab was mirrored on the prof's screen in class.

The one last thing I have been really noticing is how much everytime I interact with technology, I envision the code underneath. While watching someone play solitaire, I imagine how the computer stores and displays the information (a list could hold the cards randomly assigned to each line, and another list holds the remaining deck; rules govern whether cards can be moved from a list to another based on stored variables). I look forward to a chance to see how we create the graphical interfaces used by programs in future CS courses. For now, I can simply ponder the code behind it all.

No comments:

Post a Comment