Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, September 8, 2010

The First Programming Language

Jacquard Loom Punch Cards, by Lars Olaussen
The first programs were written for a loom designed by Joseph Marie Jacquard (1752-1834).  The programs consisted of a series of holes punched into a card.  The machine reads each row of holes, corresponding to one row of thread in the design being woven, and uses the pattern to determine which hooks should be used for that row.  It's an ingenious system, and one that has been used for hundreds of years in the weaving industry.  Variations on these punched cards were used in early digital computers in the 20th century.

Punched cards such as these bear a close resemblance to what we today consider "machine code".  They are in essence a sequence of instructions in a language that can be directly executed by a machine.  They're not terribly easy for humans to understand.  Modern computer programming uses higher-level languages, which are then translated into machine code.  An example of a higher-level language is the Logo language I discussed earlier.

Wired Magazine has an interesting story on the Jacquard loom and its place in history.

Update: Here's a link to a BBC story I had been trying to find earlier about the lace industry in UK and their use  of Jacquard looms to this day.

Programming as Problem Solving

The programming process
I highly recommend the book Computers Ltd.: What They Really Can't Do as a good introduction to the concepts of computability and complexity from a non-technical perspective.  The figure at right is inspired by a diagram the book uses to introduce the concepts of programming and programming languages.

When a programmer has an idea for developing a new piece of software, the first thing he does is express this idea as a algorithm.  An algorithm is a method for solving a problem in a step-by-step fashion.  The next step is to implement this algorithm in a high-level programming language; this is a language designed to make it easy for a human to tell the computer what he wants it to do.  The programmer then uses a special program called a compiler to translate this language into machine code, which is a series of instructions the computer can understand.  The result is a software program that can be run on a machine, such as a laptop or a smart phone.

From this description, you can see programming is a two-step process, of first designing an algorithm and then writing it in a programming language.  Algorithm design is usually the hardest part; computer scientists often refer to the need to train students to think algorithmically.  A good definition of what we mean by this is found in Developing Algorithmic Thinking with Alice, a paper by Cooper et al. that was published in the 2000 Information Systems Education Conference:
1) state a problem clearly, 2) break the problem down into a number of well-defined smaller problems, and 3) devise a step-by-step solution to solve each of the sub-tasks
This is an extremely important skill that transfers over into many disciplines, and the reason why we believe most students can benefit from taking at least one programming class sometime in their education.

Friday, September 3, 2010

Voyages of Discovery

A quick follow-up on my earlier post regarding programming and poetry.  An old friend, Jonas Karlsson, commented to me via Facebook:
Alas, we are still bound by the constraints of hardware, interface design, and computational complexity (and not to mention our own capabilities). We are not so much god-like creators, as explorers of the possibility-space.
This is a great counterpoint -- I went a little too far in stretching the analogy between programming and creation.  I think Jonas is right that programming is more like exploring.  Like Magellan or Columbus, we have limitations we must work around, so the space we explore is not infinite, though it appears exceedingly large.  There are so many things we can learn.

One of the ways computing explores the possibility-space is through simulation, in which a part of the world, or perhaps an imaginary world, is modeled and analyzed to answer questions about what might happen in a range of scenarios.  Simulations can be used to explore population genetics, predator-prey relationships, the weather, and many other important scientific and engineering topics. 

Shortly after the Deepwater Horizon oil spill, UCAR created a simulation showing how the oil could spread, based on models of ocean currents:



Did the Age of Exploration ever end, or did we just move on to new frontiers?

Elementary Programming

Since we'll be talking about computing so much, it might be helpful to have a basic understanding of how programming works.  A simple way to get a little exposure is to use a variant of the Logo programming language.  I'll show you a few programs using KTurtle, which is part of the KDE Education Project.

KTurtle supports a very limited set of Logo known informally as turtle graphics.  The programmer controls a turtle with a pen.  The most basic commands tell the turtle to pick up the pen, put down the pen, move forward, and turn.  Believe it or not, you can draw some complex patterns with these simple commands.

Here's the first program:
repeat 5 {
    forward 100
    turnright 72
}
This tells the turtle to move forward 100 spaces and turn right 72°.  This sequence is repeated five times, resulting in this drawing:


Now put all of this in another repeat loop:

repeat 10 {
    repeat 5 {
        forward 100
        turnright 72
    }
    forward 1
    turnright 36
}
This draws the pentagon 10 times.  Each time the pentagon is drawn, the turtle moves forward 1 space and turns right 36°, resulting in this pattern:


Notice that if the product of the first repeat value and the last turnright value equal 360, the turtle will move in a circle as it completes the drawing.  The pattern above was done with factors of 10 and 36.  Here's what happens if we reverse the factors, turning 10° and repeating 36 times:


And here's the pattern that results from turning only 1° and repeating 360 times:


Looks a little like HAL 9000, doesn't it?

Now, what happens if you want the turtle to turn a different amount each time through a loop?  This is where you will need to use a variable to store the current amount you would like the turtle to turn:
repeat 4 {
    for $x = 1 to 100 {
        forward 10
        turnright $x
    }
    turnright 80
}
The for statement tells the turtle to set the variable $x to 1, move forward 10 steps, turn right x°, then repeat the loop with $x set to 2.  This continues for every value from 1 to 100.

These steps are themselves inside of a while loop that repeats the drawing four times, turning right 80° after each iteration, to produce this figure:


You can do a lot more than this with most programming languages, but this hopefully this gives you a general idea of what programming is like.  The KTurtle manual provides a complete description of the commands available.

If this looks like fun, you might try the Scratch software developed by MIT to provide a visual way of creating a much greater variety of programs.

Thursday, September 2, 2010

Programmer or Poet?

In his book The Mythical Man-Month, Fred Brooks shares this insight about programmers:
The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.
Few would expect a man with a Ph.D. in Math, who is best known for his work writing software at IBM, to wax so so eloquently about the process of writing software.  Yet this quote captures precisely the lure of programming for many who practice its art -- the idea that we can build whatever world we want, following whatever rules we imagine, and populated with creatures of our own design.  What a heady experience!  Writing a program provides a kind of thrill that is hard to replicate.  I built this.  It is all mine and I am its God.  An engineer who designs bridges is limited to the rules of nature, and must consider the stress on the structure and whether it is safe for people to drive on.  They better take into account the impact of wind on the stability of the bridge:



As my colleague Charles Knutson likes to say, computer scientists have no such limitations -- we can build a bridge that flies!

Leonardo da Vinci chose to create his own world when painting the Last Supper, one in which the apostles are engaged in a variety of activities, rather than staring piously at the viewer.  He set the rules for perspective, he chose how the characters looked and acted.


Compare this to Salvadore Dali's surrealistic rendition, which creates an entirely different world of transparency and arithmetic.


Is a programmer more a scientist or an artist?

In a physical world that is made smaller through modern transportation and communication technologies, virtual worlds continue to offer voyages of discovery.