Sunday, 27 October 2013

The Wonders of Stacks



Stack. A very simple and easy to understand data type, and a great example for the beginning programmer. It is a type that is useful for introducing the concept of Abstract Data Types to a beginner, however to the more experienced programmer, Stacks can be even more useful.

Consider the problem of a calculator. We should all know how to program at least a simple one that can carry out basic operations along with those in any standard math library, however when parentheses are introduced the problem becomes more challenging.

This is just one of many uses for a stack. It can be used to implement Djikstra's Railway Algorithm for a more complex solution (Operator Precedence), or it can be used in a simpler case to parse the contents of the parentheses.

Consider (1 + 5) * 2.

If we use a stack, we can push the tokens in the expression onto it until we meet a close parentheses ')', At this point we can now pop all of the items in the stack until we reach the open parentheses '('. Now we can simply evaluate the expression, push the result back onto the stack and continue on, allowing us to simplify our problem.

Wether you're an experienced programmer, or a beginner, a stack can be a useful thing indeed.

Monday, 14 October 2013



     Object oriented programming. a familiar technique to some, a foreign one to others. It depends on what language a person is used to. To those familiar to it, Object Oriented Programming (OOP) is a very natural, very easy way to solve problems. It allows the programmer to take a concept they're used to and represent it as classes, interfaces, and objects. Classes can have attributes (or fields as some know them) defining the characteristics, and methods, which define its' behaviours.

      Perhaps the most useful application of this concept is in creating Abstract Data Types, or ADTs. These are objects we create and model within programming languages by representing a real world concept or object within the code. Consider a well known type, the Stack. A list where new objects can only be placed on top, and removed from the top. In this case we have taken a real world concept (A stack of items) and modelled it within a programming concept by using lists and built in functions.

What other objects can you think of that we can represent as ADTS?

Recursion - The human way of solving problems


     Recursion can be thought of as the "human way" to solve a problem. While a very useful concept in some respects, recursion is also a controversial topic within computer science. It can be an extremely useful tool for traversing certain data structures, however in many cases, (though not all) it can also be done in more efficient ways with iteration.

Recursion is the process of breaking a larger problem down into subproblems, or smaller versions of itself. This process eventually terminates in what's known as the base case, where the problem can not be simplified any further. There are many problems for which this has useful applications, for instance the towers of hanoi problem may be vastly simplified using recursion. The job of the monks of Hanoi can be vastly simplified by simply breaking the problem of moving a tower of n disks into smaller, simpler cases of moving n-1 disks until finally reaching the base case of moving a single disk.

The problem with recursion comes when efficiency is a concern. This is where it becomes a controversial topic, with some arguing that it generates numerous function calls on the stack. While this argument is true, recursion does create copies of the functions within the stack along with it's associated arguments, it is often the simplest, and in some cases the only realistic way of solving the problem.

At this point it comes down to efficiency vs. simplicity. Which do you prefer? A shorter more readable and sometimes more understandable solution, or a longer, more efficient solution?