hudebnik: (teacher-mode)
hudebnik ([personal profile] hudebnik) wrote2012-02-09 03:19 pm

Comparing programming languages

The students in my Principles of Programming Languages course have been kvetching because I asked them to do their interpreter project in Scheme. They only learned Scheme last semester, and they already know it's not a "real" language, so it's outrageous and arbitrary of me to ask them to write something substantial in it. I had pointed out that it makes parts of the assignment easier, and they replied (reasonably enough) "but if it takes more work to learn the language, we haven't really saved anything."

So yesterday in class I said "OK, I give in. This course isn't about Scheme, it's about interpreters and language features, so you're welcome to do this assignment in whatever language you like. Would you like to do it in Java?" Massive acclaim. "Let's work through Version 1 of the assignment [which they had all already done in Scheme] together in Java; once we've got that working, you can do all the subsequent versions on your own." I opened Version 1 in Scheme for reference, and started translating at the board.

The first datatype definition wasn't too hard. The 6 lines of Scheme code became 70 lines of Java, not counting blank lines and comments, but the Java code was mostly very familiar boilerplate, so that's not too bad. (Had to write constructors, getters, toString() methods, and equals() methods. Technically, should have overridden hashCode() too, but these students have never heard of hashCode().)

Then we got to a part of the Scheme code that relied on Scheme's built-in lists, which can contain anything, including other lists. Java has ArrayLists, but it's sort of a pain to work with heterogeneous and nested ArrayLists, so we started building a Java datatype to correspond to Scheme lists. That took about 90 lines of Java code, not counting comments, blank lines, and test cases. Except that the constructor wasn't very convenient to use: the students all agreed it really should take in a string of the form "(3 + (4 / 5))" or something like that. At which point the class ended.

These students haven't taken compiler construction. They've never written a lexical scanner, much less a hierarchical parser, so it didn't occur to them that this constructor, which approximates the functionality of Scheme's built-in "read" function, might be non-trivial. I just wrote it myself outside class, trying to keep things short, simple and clean: it took 88 lines for the reader itself, plus 87 for supporting data structures and 171 for test cases. (The reader is complicated enough that I really needed all those test cases, and many of them failed at least once.)

Anyway, we've just added 265 lines to the Java program to duplicate functionality that came for free in Scheme. Now we can get back to the original problem....

This will probably delay the course by a couple of days (especially if I don't give them my reader code), but if it conveys the lesson that sometimes it IS worth learning a new language in order to make the program easier, it'll be worth it.

[Followup Feb. 10:] At the beginning of class today, at least one student was already saying "let's go back to the Scheme version." I didn't; I wanted to use this program to motivate and illustrate a couple of Java programming patterns. So we got this minimal interpreter working (using the reader I had written), but we'll be back to Scheme on Monday.

[identity profile] goldsquare.livejournal.com 2012-02-09 08:39 pm (UTC)(link)
I wish I'd learned Scheme in my day...

And if those kids want to re-implement Scheme in Java (at least the relevant parts), best of luck to them. :-)

I can dig a ditch with a backhoe, or a spoon... so a backhoe and a spoon are Turing equivalent, right? :-)

[identity profile] goldsquare.livejournal.com 2012-02-09 10:13 pm (UTC)(link)
Disturbingly close to truth, that one.

Sometimes I think Scheme and other functional languages are an angry response to the Franz Lisp documentation for the if predicate...

I swear to God, I read that page forty-leven times and never understood it. So I just started picking examples out of extant code. :-)

[identity profile] hudebnik.livejournal.com 2012-02-10 01:14 am (UTC)(link)
Actually, Scheme IS in large part an angry response to Common Lisp. The members of the original Scheme committee bragged that their entire language standard was shorter than the index to the Common Lisp language standard.

[identity profile] goldsquare.livejournal.com 2012-02-09 10:15 pm (UTC)(link)
Also: "Oh look: I can see Python from here..."

[identity profile] hudebnik.livejournal.com 2012-02-10 01:22 am (UTC)(link)
I've played with Python a little on my own, but never used it in the classroom (although, based on what I see at CS education conferences, if I had to bet what will succeed Java as the "dominant" language for introductory programming classes, I would bet on Python).

I did, however, spend a month of last semester teaching (and learning) Ruby, which as far as I can tell does everything Python does, plus continuations (although I think higher-order functions are easier in Python than in Ruby).
Edited 2012-02-10 01:23 (UTC)