Friday, January 28, 2011

Math Through Computer Programming

I remember liking Geometric constructions and proofs in high school, but even for those of us who picked it up quick, a little was enough. Supposedly, geometric proofs teach you how to think logically; there's a story that young Abe Lincoln bought a copy of Euclid and practiced proofs to hone his sharp logical mind. For those students who don't understand proofs, they're unnecessary torture devised by math teachers. And anyway, that was then.

Now there's these things called computers that will do amazing things if you tell them exactly how to do them. The operative word there is "exactly." Computers have been described as "stupid" because they have no common sense; they can't understand what you want them to do unless you tell them every little step and spell everything correctly. To me this is a description of the ultimate logic-learning opportunity.

Years ago I was introduced to the power of computers to teach logical thinking by Seymour Papert's incredible book Mindstorms. Papert and his team developed the LOGO programming language in 1967 and a few years later it was used to instruct robot turtles to draw figures by walking around the floor holding retractable pens. I even bought a robot turtle with the intention of getting it to draw stuff, but never got it to work correctly. The current version of StarLOGO still retains the turtle analogy, but the turtles are virtual and can move lightning fast onscreen.

I've used StarLOGO in classrooms to teach kids how to do simple programming, and at the very least, they're impressed at how cool it is to make the multicolored turtle army zip around leaving trails. As discussed in Mindstorms, just about every idea in math can be approached using the turtles for illustration, and the elementary ideas in computer programming can, too.

One of my favorite things in the book is the idea of the procedure or subroutine. Once you teach a turtle how to make a square, for example, you can save those lines of code as the instructions for how to "square" and instead of typing it all out again you can just tell the turtle, "square." In any program after that, when you need to make a square, one word will bring up the entire "square" subroutine. It might be a lot of work at first, but once you have it, you never have to do all that work again.

The loop is one of the most useful and elegant concepts in all of computing. Teach a turtle to square, then use a loop ("repeat" a number of times) to have it make a square, then turn a little to the right and make another square, and so on. These few lines of code:
to neato
repeat 50 [square rt 5]
end
will produce an unexpectedly complex figure:

Of course, the discovery every student makes about the real process of writing programs is that it never works right the first time! (My figure above didn't, either: it was too big for the window, so I had to go back and shorten the side of the square.) Debugging is an unavoidable task for the novice programmer and expert alike, but some students and adults who are used to getting what they want through interpersonal means interpret these inevitable glitches to mean "the computer hates me."

I was recently reminded of the power of computer programming to teach math ideas by Danny Kodicek's book Mathematics and Physics for Programmers. Cambridge University graduate Kodicek presents math ideas from integers and arithmetic through matrices, calculus and differential equations in ways computer programmers might encounter them in the course of programming video games, for example.

The code in the book is actually pseudocode, since it's a "verbose" style of the "Lingo" language used primarily in Macromedia Director. For a novice programmer like me that doesn't detract from the author's ability to show the logic behind the code for a certain task and also the logic of the "shorthand" that has evolved into actual computer languages.

Just like stepping back and asking myself how I would teach a turtle to draw a square, Kodicek's book made me think about how to rotate the square, and more.

No comments:

Post a Comment