Friday, September 3, 2010

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.

2 comments:

James Wilcox said...

It is so crazy to think that basic programing has become so easy that even a child can begin to tinker and play with it. I can only imagine the creative potential released in a child who has learned to program as freely as he has learned to talk and write by hand.

Ariel said...

Whoa--I totally did this back in the 7th or 8th grade in my Computer Applications class! Back then I had absolutely no idea what it was for and why I was moving this little turtle around. I believe it is an excellent starting point for learning programming concepts--you just have to build on the basic knowledge gained from this program (which obviously, I didn't).

Post a Comment