Monday, June 30, 2008

Fischer Price My First Lisp

I finished my first real CL program today. I use the word program loosely because it was really just a loose assortment of functions.

That's the way I approach programming nowadays: quick little hacks that get the job done. Why settle for elegance when you have duct tape?

Ironically, I got the idea for my code from this:

(defun rename-files (from to)
(dolist (file (directory from))
(rename-file file (translate-pathname file from to))))

I found that code snippet in a pretty recent entry from On Code. It's a mass rename function that takes all files with filename from (yes, you can use regular expressions as input) and renames them to filename to. Pretty nifty for 3 lines, right?

Well anyways... back to my program. I wrote it to consolidate large numbers of csv files. Basically I have over a hundred or so tables that associate a value with two primary keys (guess they're not really primary.) While the primary keys can vary, the majority of them stay constant across files.
Something like:




















pkey1apkey1bvalue
pkey2apkey2bvalue
pkey3apkey3bvalue
pkey4apkey4bvalue
and then you'll have another table like:




















pkey1apkey1bvalue
pkey3apkey3bvalue
pkey4apkey4bvalue
pkey5apkey5bvalue
So what I want to end up with is:































pkey1apkey1bvaluevalue
pkey2apkey2bvaluenull
pkey3apkey3bvaluevalue
pkey4apkey4bvaluevalue
pkey5apkey5bnullvalue

Not a particularly difficult problem, but it's always exciting to use LISP in real life.

One thing it got me thinking about was that eternal war between arrays and linked lists. I used list matrices (list of lists) to represent the tables. In retrospect, this was probably a lazy move that would have cost me crucial efficiency points had the tables been larger by a factor of 10. Or maybe not. By using arrays, I get random access at the cost of re-allocating the arrays every time I have to increase the size of the tables.

Something to think about I guess.

No comments: