Sunday, August 28, 2011

A new journey: Lisp

The first computer science course I ever took was at Siena College, and it was taught with Scheme.  The year was 1997.  The course was probably called something like "Comp 1" or "Intro to CS."  Back then, I knew nothing about programming or computer science, and I didn't give the school's choice of using Scheme a second thought. Every subsequent CS course at Siena was taught using C++.  The year after I graduated, I heard C++ was dropped in favor of Java.

During my sophomore year, I learned Java on the side in order to land an internship at General Electric Power Systems.  Then I graduated, and I continued to leverage primarily Java in my professional life.  I attained several Java certifications and did a fair amount of work in the JEE space.  I did a smattering of development in C++ and did some exploration in VB.NET, but for the most part I was focused on Java.

Fast-forward to 2010.  Java is still my primarily language, and I'm quite good at it.  I'm well read on the likes of Martin Fowler, Joshua Bloch, Brian Goetz, etc.  I keep my GoF book close to me at all times for good reference.

Then something changed.  I stumbled upon the following web pages:

Beating the Averages, by Paul Graham
Can Your Programming Language Do This? by Joel Spolsky
Execution in the Kingdom of Nouns, by Steve Yegge

Steve's is pretty humorous, and, if nothing else gets you wondering about functional programming.  Same with Joel's.  Joel's articles are unparalleled --- they are extremely fun, and enlightening to read.  His too raises your eyebrow about functional programming.

Paul's article blew me away.  It's a game changer.  Paul's article is specifically what prompted me to learn Lisp.  I want to truly learn the language, and put to the test if it really is as powerful as he (and others), say it is.

So I'm learning 2 Lisp dialects at the moment: Clojure and Common Lisp.  My plan is to get good enough with Lisp to the point where I can one day call myself a "Lisp Hacker."  In my mind, I've divided my Lisp journey into 2 destinations: the Lisp language itself, and macros.  My hope is that by next year sometime, I'll dive into macros.  For the time being however, I'm simply trying to master the "core" language itself.

The best way to learn a new programming language is to use it.  And so with that said, the following are some code snippets of my solution to Google's Store Credit code jam problem.  The code is up on GitHub:

This snippet contains a function whose purpose in life is to read-in the contents of an input file, parse it, and return a data structure containing the test case data. The idea is to "transform" the data, as it was structured in the file, to a generic format more suitable for downstream processing. I.e., the code containing the actual "business logic" should be isolated from the logic of parsing a file. Furthermore, the design of the data structure used to "house" the data should not be influenced on how the data is stored in the file.

And this snippet contains the logic that solves the actual "store credit" problem:

There's a README file packaged with the project that explains how to actually run the program.

I will definitely admit that learning Lisp has not been easy so far --- having worked my entire adult life to-date using an imperative language like Java has definitely "conditioned" my mind when it comes to thinking through solutions to problems. But with that said, I'm having a complete blast learning Lisp and absolutely look forward to the journey.