Wednesday, October 31, 2007

Fun with Scheme

As I've come across the time, I've watched some of the videos (and read the book) of "Structure and Interpretation of Computer Programs."

I've been using Petite Chez Scheme along with Textpad for my little learning exercises (some of the other implementations of Scheme seemed rather difficult to get working on windows).

It's amazing how little there is in common between programming with a lisp language and mainstream object oriented code monkeying. Sure, C# has anonymous methods, but the fact that they're available doesn't change much (although who knows how far the DLR will take mainstream .Net developers down the dynamic language path).

In one of the first lectures, Gerald Jay Sussman challenges the students to come up with an iterative rather than the commonly taught recursive solution to the famous "towers of hanoi" puzzle. It turned out to be quite a little challenge for me; both discovering the algorithm and translating it into code the Scheme way.

The first thing that I had to struggle with was (of course) all the parens. Absolutely everything is inside pairs of parentheses. I don't really understand the exact reason for this (it somehow enables easy creation of programs using declarative data), but one thing is for sure - it's really easy to learn the syntax of the language (and there isn't a ton of crazy tricks as in C++ whereby on might use the language for years before discovering some). Making sure your paren pairs are matched up takes a bit of effort. It seemed like I needed to transform into a human compiler in order to write a function with much length, but after awhile it's not so bad. Conforming to the indenting conventions makes it easier, but it sure seems to make it difficult to cut and paste code or insert a comment at the right spot.

The next thing that threw me was the fact that there are no reference parameters in Scheme; they are always by value. At one point I was searching all around trying to figure out how to pass parameters by reference, and couldn't find a way before I finally figured out that there is no way. I'm pretty sure it's purposeful: functions produce no side effects.

The last main hurdle to overcome is the fact that you're not supposed to declare and set variables as a rule. Hell, you can't pass the suckers to functions, so what's the point anyway? I learned that one the hard way. I created my entire program (managing to test each function as I went along) only to discover that the last link was impossible. My practice of using variables in the mainstream way painted me into a corner. Instead of declaring variables, you create let blocks with the variable's value declared at the beginning of the block. The language help says that let blocks are just another form of lambdas which is pretty cool (everything is functional).

Here was the easy part: using lists. There are some funky things about lists that I don't quite understand, but one thing was easy: stuffing whatever I wanted in there. I wanted a string to represent the name of each tower to be stored along with the integer values of the rings on that tower. With OO, you'd have to either define a type for such list or do some casting of objects coming out of a generic collection, but neither is necessary in Scheme. The same would hold true with any dynamic language, of course.

Overall, it was a pretty fun exercise. It'd be neat if I could use functional programming at work. With the DLR, that just might be possible (I could at least write test code or internal utilities or something).