Wed, 07 Jun 2006
Ruby vs. Python (vs. Perl) (vs. Lisp)
I've recently been playing with Ruby, and I think I've finally found a replacement for my various uses of Perl. For a while now, I've been using one of two languages for my at-home programming: Common Lisp for the more complex or interesting tasks; and Perl for the simpler tasks, standalone programs, and one-line text processing.
I've long had a love/hate relationship with Perl. On the one hand, its
DWIM approach to things make it easy to do simple things, especially
one-off text munging.
perl -pe '<stuff>' has long been my friend. It
also has CPAN going for it; many of my problems have been solved with a
CPAN module and a little glue code. On the other hand, Perl tends towards
extreme ugliness, especially when the code starts to get complicated.
I've never liked the OO interface for Perl, for example; not only does it
feel very obviously bolted on to an existing infrastructure, it didn't
even get the sharp, pointy edges sanded down when they did it.
Thus it was that I was impressed with Python when I first met it. It
felt like a language designed with OO from the bottom up. (But it wasn't;
the addition of OO stuff had just been done in a much better way than
Perl's.) It had nice data introspection. It had functions as first-class
objects (so they got the benefit of introspection, as well as the other
benefits of first-classness). I figured I could live with the
whitespace-as-syntax wart. As I got to know it more, Python started
falling down in a few places. For one thing, Guido van Rossum apparently
doesn't like functional programming. Python has some functional
programming constructs, like map and filter, but Guido keeps threatening
to remove them. There's no good way to create anonymous functions,
lambda is only useful for relatively trivial purposes.
Python is also not terribly useful from the command line, largely because
of the whitespace-as-syntax thing. Again, it works for some trivial
cases, but is not general purpose enough.
So it was that I resisted bothering with Ruby for a while. "Oh," I said, "It's just another fad, like Python." But I kept hearing good things about it, even apart from all the buzz that Ruby on Rails has generated. So I finally resolved to poke at it a bit, and I'm pretty impressed. It's a much cleaner language than Perl, but it's better at functional and command-line usage than Python. It's enough to make me want to switch to Ruby for all my non-Lisp needs.
There are some things that feel like they might bother me, though. While Ruby not only supports anonymous functions but uses anonymous-function-like blocks all over the place, functions in Ruby are not first class objects, which means that you can't quite sling them around as easily as data. The use of special characters to determine variable scope (globals, class variables, instance variables, etc.) seems a bit weird to me, but at least it's not as bad as Perl's variable special characters.
Basically, Python has too much insistence on doing things its own particular, sometimes convoluted way. Ruby stole all the useful things from Perl while otherwise maintaining a very clean language that doesn't spit on my Lisp-influenced thinking.
Doing Your Own Math
When I was going to Catonsville Community College (now the Catonsville Campus of The Community College of Baltimore County), I took a couple of computer science courses. The first of those was essentially an introduction-to-programming course, with the textbook using C. The professor, however, said that he would accept programs in any language that was cleared with him beforehand. Since I already knew C, I used the class as an opportunity to learn Common Lisp.
One of the problems from the course that I still remember fondly was the "do your own math" one. We were allowed to have a function that used the language's built-in addition operator to add one to a supplied value, and the same for the subtraction operator. Building on those two functions (i.e. not using any of the language's other math operators or functions), we had to write functions to implement addition, subtraction, multiplication, division, modulus, exponentiation, greatest common divisor, and least common multiple. To simplify things, we only had to make the functions work in the domain of positive integers (so the division would be integer divisions). Because I felt like setting myself a little more of a challenge, I added a couple of my own constraints. I decided that I would make my functions work for all integers, both positive and negative, and that I would match the interfaces for the Common Lisp functions I was replacing (at least as far as integers were concerned).
I found the problem to be quite interesting because it involves breaking down common mathematical operations into their basic forms. It's also rather neat to build up a hierarchy of common routines where you've constructed each layer yourself and you end up with a tower where you've built each layer, aside from some small contributions for the foundation.
I was bored recently and decided to redo the problem, now that I've got more Common Lisp experience under my belt. In particular, I realized that I could make a new package and shadow the functions that I was replacing, so all of my code would look normal, even though I was using my own functions. I also thought it would be interesting to compare my Common Lisp programming from nearly a decade ago with my Common Lisp programming now. Here's my original code and my new code.
Probably my biggest surprise was that my original version of least common multiple gave wrong answers when supplied with multiple arguments. I clearly hadn't tested it enough in school. What's interesting is that I originally tried the same approach with my recent code, but had to fix it when I saw that it wasn't right. My recent code is also more efficient; division is orders of magnitude faster, especially for small divisors, and my old code triggers a stack overflow when asked to multiply large numbers. Structurally speaking, the newer stuff tends to be more compact; helper functions are declared with labels rather than at the toplevel, and I make better use of some Common Lisp functions, most notably reduce.
So now I've gotten even more out of the interesting problem. In addition to the fun of solving it at all, I also get a comparison of changes in my programming style over time. Perhaps I'll address it again in another decade or so.