Basic Lisp Syntax (or Why Lisp Hurts My Brain)

This week I’ll be getting into some of the basics of the language. I’ll also explore some of the syntax differences that make Lisp look so funny. Rather than just listing all the features of the language, I’ll use some examples and explain how each works. Without further delay, let’s take a look at a basic piece of Lisp code.

(setf x (* (+ 10 7) 10))

That adds the numbers 10 and 7, multiplies the result by 10 and assigns the value to the variable x. When executed at the Lisp prompt it will run those steps and output the computed value of 170. Just in case the English definition isn’t clear, here’s the same thing in C/C++/C# minus the output step:

int x = (10 + 7) * 10;

Even with this single line we can see some of the things that make Lisp different. The first thing that strikes me as foreign is the notation. The arithmetic and algebra I’ve been learning since grade school is in infix notation. Most of the popular programming languages follow this convention so program statements generally look how we’d expect a basic mathematical expression to look. Lisp uses prefix notation. Note: Normally in prefix notation parenthesis are superfluous, but with Lisp the operators can take different numbers of arguments. The parenthesis let Lisp know where an expression begins and ends. (If needed, here’s a refresher on prefix, infix, and postfix notation.

Lisp consists of atoms and lists. What one would generally consider data types such as strings or integers are atoms. Lists are a collection of atoms, lists or both. One data type Lisp has that seems entirely different to me is the symbol (which is another type of atom). Paul Graham states in his book ANSI Common Lisp “symbols are words.” I’m not entirely sure what that means, but here’s what my AI professor told us. He stated that symbols are merely things to reference a concept. Just as the word “dog” is a symbol to reference the concept of type a four legged mammal in meat space. I’ll have to get further along before I can really understand the use of this data type.

Lisp programs themselves are expressed as lists, which is another reason the language looks so funky. This seems to be the point, on which most Lisp hackers agree, that makes the language so powerful. Lisp programs have the ability to manipulate themselves and even write new Lisp programs. While I’m sure it will be a little while before I get to this level, this aspect of the language excites me more than any other.

I’ve only scratched the surface, but it seems to be slow going at first with this language. In the meantime, here’s a little more code to stare at until I write something more substantial over the next week.

(defun myfactorial (x)
   (cond
     ((= x 1) 1)
     (t (* x (myfactorial (- x 1))))))

Want to run the code in this article and play around with Lisp? Lispbox is probably the fastest and easiest way to get there. Simply unpack and run.

ack!

that should be here. Sorry about that.

The other one is broken,

The other one is broken, too. :D

Damn, I'm not sure what

Damn, I'm not sure what happened there. I just checked my original source text and the links are correct. Maybe something with hitting the preview when I posted? Anyway, here's the link on prefix, infix and postfix notation again.

Bad Quotes

I've updated the links but I suspect that you wrote it in something that puts all kinds of garbage characters. If you can use or copy/paste through notepad you'll be in good shape.