Lisp – wow, that’s a weird feeling.

October 25, 2008

I’ve been holed up in my office lately, teaching myself Common Lisp for an upcoming part of my Ph.D, and I have to tell ya, it’s a weird feeling to write code in Lisp.  I read through Peter Seibel’s great online book Practical Common Lisp, which is an absolutely brilliant resource for first time Lispers, especially those coming from other languages.  (I will admit to a bit of irony, though:  I downloaded the book and wrote a Python script to HTMLize the footnotes into links so that I could jump back and forth without having to continually figure out where the hell I had been when I went to read that note).

In any case, I just finished my first “real” program in Lisp – real in that it was done entirely on my own and required more than 5 lines of code, though the problem I “solved” was an imaginary one.  It’s one of those throwaway efforts to acquaint yourself with a new way of thinking.  I went into it thinking that Lisp was overblown, one of those things that people looked back on fondly with rose-colored glasses while muttering about how “real programmers used to do it”, but by the time I was finished writing the thing, I found that I was actually enjoying myself.  I can’t even explain why, but by the end of writing the program, I was actively looking for new ways to extend the idea so that I could keep writing code!  And now, I find myself looking forward to my next program in Lisp, a far cry from where I started.

(If you’re wondering what I was writing, it’s kind of embarassing, but here it is:  I wrote a program to quantify the cost to typing words on a keyboard from the perspective of a single-finger, one-handed typist.  The “cost” is distance – i.e. how far would the finger have to travel – defined by adjacent keys, so that ‘T’ and ‘E’ are two units apart (‘T’ -> ‘R’ -> ‘E’).  I did this using cl-graph to map the keys onto a graph, and wrote an implementation of Djikstra’s algorithm to calculate the distances between key pairs.  I could then calculate the distance between each pair of letters in a word and sum the distances to get the total cost, which I then averaged over the length of the word so that the penalty for long words was minimized.  After that, it was a snap to write a couple of functions to do things like take in a word list and write out each word with its associated cost, or to get the total cost of a string.  See?  Told you it was trivial. 🙂 )