Daniel Pitts’ Tech Blog

Archive for the ‘Lisp’ Category

Learning to code with a speech impediment: Or, What’s all the buzz about Lisp?

Monday, October 8th, 2007

Although I started by journey through Computer Science much later than many people (in 1989), I feel that I’ve experienced the evolution of paradigms, recreated just for me.

I started out when I was 8, programming in BASIC on the Atari 800. The paradigm there was “unstructured programming”. I used line numbers and *gasp* GOTO to manage flow control, had no idea what a sub-routine was, and used one letter variable names. I even learned the hard way that you needed to preload DOS before you start your project, or you’ll never be able to save it.

Then I learn structured programming in QBasic. These things called for/next and do/while and even subroutines! Line numbers were no longer necessary or desirable, and Labels became common place. I even got into some basic data encapsulation (not abstraction), but using the basic structure mechanism QBasic offered.

After that, I learned to program in C. Not much of a difference though. Sure it was lower level, and ran 50 times faster. I could access the VGA hardware directly and do some pretty nifty things. Around the same time I also began to learn Assembly. This brought me back to GOTO to manage flow control, but it fit for the language.

Eventually, I learned C++. Wow, this is great! A much better C! I didn’t understand most OO principals at the time. so I was still stuck in Structured/Procedural land.

I started studying Java. One day, an epiphany! I (thought I) understood what OOP was all about. Combine related data into one place, and the procedures that affect that data also go in one place. It made so much sense, I couldn’t imagine how I thought it was any other way.

Well, then I started to get really complicated programs, and couldn’t figure out why they were so brittle. Oh, whats this about Patterns and Refactoring? Ah, I see. I should abstract the data and behavior more fully. I should use polymorphism! Duh, why didn’t I understand this before! Strategy and State, Dependency Injection, Command, Flyweight, how I missed you and didn’t even know it!

That was my most recent epiphany. I realized even as it occurred that there had to be more. There had to be a better way to do things. Guess what. There is.

Enter: Lisp. Keep in mind that I’m still very new at Lisp, and this is about my journey and impressions.

The attention to Lisp has been centered in academic circles for a long time. Originally conceived of in the 50’s by John McCarthy (more history). It is an elegant language that focused on possibilities, not syntax. Many people see some example Lisp, and their first thoughts are generally “What’s with all those parenthesis?” I have a counter though. Look at some Java code, and ask “Whats with all the dots, commas, brackets, braces, ambiguities, and weird structure?”

The core of Lisp is to provide very small building blocks to work with. In this regard its much like a low-level language, except for the fact that it doesn’t mirror machine-language very closely. With these building blocks, its surprisingly easy to adopt whatever paradigm best fits your problem.

You can choose between Functional and Procedural programming (and even take the hybrid approach). You can use OO design or not. You have the ability to use Closures (passing and returning Functions/Procedures as values).

You can even treat code as data. This might not sound so impressive, but think about all of the repetitive code you’ve had to write or copy’n’paste. Think about how often you’ve thought “Maybe I could write a program to help me write this program.” For those of you who’ve used C, think about all the #define hacks you’ve created. Now, realize that Lisp lets you do this inline, and in lisp! Using something called “defmacro”, you can create a macro that writes your program for you! And most of the time, its as easy as simple list manipulation.

So, what brought me to Lisp in the first place? I was thinking about creating a programming language that handled all of the special things that I wanted. I wrote a post on the newsgroup comp.object that detailed a brain dump of all the features I wanted. I had a few replies that either criticized a feature, or pointed out that Lisp supported a feature already. I did a little investigation and found that not only did Lisp support all the features I wrote about, it supported more! And on top of that it was extensible (with defmacro).

Well, I’m on this journey for better or worse. If I find that Lisp is best left to the Academics, so be it. I enjoy academia myself, and I’m sure that I’ll find away to apply the principals of Lisp to write better code in other languages. If I find out that Lisp is *the* language, then all the better.