« Up and Running with Lisp | Main | Moving to Movable Type »
September 17, 2004
The Road to Lisp
The ALU CLiki site has an interesting entry called The Road to Lisp and I’ve been thinking, this might be an interesting story for me to tell, though not so much to members of the clan.
When I got to Tech, during the summer between the first and second years (I believe) I met Allan Poindexter. In discussing programming languages, he talked about Lisp, being a Lisp programmer as he was in a former life, and about the power of the Lisp machines. This all seemed just amazing to me. He told me to buy The Little Lisper, which I still own and I’m waiting to get back from a friend. Unfortunately, it didn’t really gel in my mind—I was still learning Python and quite content with it.
The next year, I took CS 324 “Principles of Programming Languages” with the famous and esteemed Hamdy Soliman. It was my favorite class, probably still is my favorite class, and definitely the aspect of the curriculum I’m most likely to keep up on. I slept a lot in that class, not because I was getting straight-A’s, but because I got most of the general concepts, and that was good enough for me. At the end of the class, we got into the “weird” languages which for the most part define their own categories: Lisp, Smalltalk and Prolog. I recall one day when I was dozing and Hamdy called on me to have me answer a rudimentary Lisp question, something along the lines of (cdr (car (cdr ‘(a (b c) ((d) (e f)))))). I said “What?” in a confused way, because he had woken me up, and then quickly said “Oh, C,” before he had a chance to chastise me for sleeping. Hamdy let me doze from then on. This was one of the few moments of glory for me at Tech at all. :)
This reminds me of another story, but I’ll make it brief. Hamdy had a pet peeve: he didn’t want to see or hear anyone asleep, it just pissed him off. However, if you were talking, it meant you were learning: you were stimulated from words of wisdom and glory flowing gently from his teeming storehouse of knowledge. Mazumdar, on the other hand, couldn’t stand to hear anyone talking. You could sleep, it was your own funeral when you couldn’t pass the tests or do the homework, but if you were talking, he’d hear your words and he’d start speaking them, and then he’d be confused and flustered, so he usually just got fluster right when someone started talking, and told them to please be quiet.
One day in Databases with Mazumdar, he was going over some really dry topic, I think built in functions in Oracle (how rote!). Baird was sitting to my left and Mat to my right. I tried to speak with Mat, but we kept making each other laugh, and thanks to the giggle loop, it was really insipid. The first time, Mazzy glared at us and stopped talking for a few seconds, then continued. The second time, Mazzy shouted “Matthew, are you alright? Is something wrong?” Mat sheepishly said, no, and then I decided for the advancement of human knowledge, I would try not to write anything to him anymore this class. (That, or he refused to take the pad). I turned to my left and tried to say something to Baird. Baird apparently had been sleeping, woke up with a start saying “What?!” nice and loud. Mazzy was definitely staring at us and had shut up, and then Baird looked down and started laughing really loud. “Is something wrong David?” “No, Dr. Mazumdar, I just seem to have drooled all over myself. Ha ha ha!” Mazzy glared at me, and I gave up and went to sleep.
So, my road to Lisp. Being someone with ears, I heard the words of Paul Graham. Allan and I continued to discuss the merits and flaws of Lisp. When I got my new Contoured keyboard, I decided I was going to fix three of the big problems in my life: using an inferior keyboard, using an inferior keyboard layout, and using an inferior editor. So now I am happily using Dvorak and Emacs to write this, on my laptop’s keyboard because I left the good one at work. At any rate, I guess I’ve been a Lisp apologist and promoter for some time, though I still teach people Python and direct people to Why’s Poignant Guide to Ruby as often as I can. When I told Alex I had selected Lisp for Soultrain, she said she wasn’t surprised. I said, what? She said, “You speak more highly of it than any other language.”
I had, in fact, been in the car with Allan, her and Faust just a few days earlier, going to the shitty Apple Festival in Hillsboro. On this trip she overheard Allan and me talking about Lisp, mostly me saying, gee, it seems like nobody has invented anything new or good that’s better than Lisp. From that perspective, what a terribly depressing world we live in—nobody can ever do better than X. But look at it the other way around: if you use X, nobody will ever be able to beat you! At least, in forty years and thousands of languages later, nobody has come up with anything more general and better.
I did a little algorithm for work today in Lisp, just to force myself to start thinking in Lisp. Here it is, in Lisp:
(defun adjustment (spamminess trust)
(cond
((eq spamminess 50) 50)
((< spamminess 50) (- 50 (* (- 100 spamminess) trust 1/200)))
(t (+ 50 (* spamminess trust 1/200)))))
Here’s the same code, in REALbasic (the implementation language):
Function adjustment(spamminess as Integer, trust as Integer) as Integer
Dim result as Integer
If spamminess = 50 Then
result = 50
ElseIf spamminess < 50 Then
50 - (((100 - spamminess) * trust) / 200)
Else
50 + ((spamminess * trust) / 200)
End If
Return result
End
Version | Lines of Code | Adjusted Lines of Code |
REALbasic | 12 | 10 |
Lisp | 5 | 5 |
Ironically, the Lisp there is considered imminently readable, and has no extraneous whitespace. The math is basically the same in both versions, though it is prefix in Lisp, which enables the (* x y z) trick that demands an extra operator in RB. But the control code is much terser, yet completely unambiguous unlike, say, Perl. Any Lisper would be able to glance at that and go “Oh, it does some hairy math. It pushes things towards 50.” Ah well.
Another Shorty
I wrote the bulk of the above the day before yesterday. Yesterday I had to write a PHP script which would select the same 3 columns out of two tables, and format each one separately according to slightly different presentation rules. I came up with this for the Lisp version when I got home:
(defun show-tables (formatter table base-price)
(cond
((null table) t)
(t (progn
(funcall formatter (caar table) (cadar table) (caddar table) base-price)
(show-tables formatter (cdr table) base-price)))))
This engine would then enable me to fetch out of the database into a list of 3-tuples, which I would then append the base price from an earlier query, and then each function can deal with its own output in its own unique way. It might look something like this:
(show-tables #'(lambda (amount qdiscount pdiscount base-price)
(format t "<tr><td>~D</td><td>$~,2F</td><td>$~,2F</td></tr>~%"
amount
(* amount qdiscount pdiscount base-price 1/12)
(* amount qdiscount pdiscount base-price))))
Now the printing logic is separate from the database logic, as it should be. :) This also makes the code about 1/2 the size, and it would also increase in size less rapidly than the PHP for comparable effect.
Book Deal
I’ve decided I need to write a book about programming languages. Because it’s something I do, and I know a shit-ton more about them than really anyone I know. True, a boast, but at the same time, I know I’ve said, “What do you know about Language X” to everyone from Allan to Hamdy at one point or another and drawn a blank. So I’m thinking I should get busy writing this information down. Not in a CS 324 text kind of way, but more as in a survey kind of way.
This, of course, leads to the next Big Implementation Question of the moment: what should I use for typesetting the document?
- Lout is one of the better typesetting languages. It has a pretty major shortcoming though—it doesn’t do HTML output at all.
- LaTeX, the famous. I got most of my friends into it for classes, and now I don’t like it so much. I refuse to rely on latex2html for anything but trivial documents, because it fucks up so much and really has no idea what LaTeX is, it’s just a shit-ton of regular expressions. At least it seems that way, with what it does to my macros and functions.
- Texinfo is what the GNU project depends on. I haven’t been terribly impressed with the way the output looks, but then again, it produces vanilla-ish HTML or vanilla-ish TeX depending on what you want. That puts it in my domain. Plus, editing it from Emacs is pretty well-supported. But I’m just sort of turned off by it.
- Groff. Disgusting, but apparently now does HTML and PS pretty well.
- DocBook. The only recommended XSL:fo processor is for-pay, limiting the ability to do anything useful there, but the HTML output is good. I’m just not sure if I want to invest the years of my life into customizing it that probably are necessary, especially considering it’s XSLT, which I hate, to do customization. I also sometimes think DocBook is a bit ultra-anal in terms of tags.
- reStructuredText is a wonderful system for plain text and HTML distribution, but I do know that it has an XML representation, and I sort of wonder what it would take to plug that into another toolchain for some other system. On the downside, it can be repetitive to write highly structured documents in it, because they need so much in the way of textural clues.
- RTF. Mac’s TextEdit is that good. Of course, this is ridiculous.
- Pay for a tool. What, me pay?
So, that’s basically where I’m at. If it weren’t for the document processing community’s general distaste for Groff, I probably would be considering it more seriously, but I’m sure it’s a huge hassle.
Posted by FusionGyro at September 17, 2004 06:03 AM
Trackback Pings
TrackBack URL for this entry:
http://www.clanspum.net/~fusion/blog/admin/mt-tb.cgi/3