For anyone who wants to learn lisp (or, more accurately, the Scheme derivative), you can check out Andrew Plotkin's Lists and Lists, which is a Scheme interpreter written in Inform.]]>

I heard about this awesome language from a friend in college. And it has been a joy to use, albeit a bit weird to get the syntax. Basically, instead of making a function call like foo(bar, baz, qux), you put the parenthesis out front and the function name first, with its arguments separated by white space. Here is how the aforementioned function call would look in real Lisp code:

(foo bar baz qux)

This means take foo, and apply it to the arguments bar baz and qux. Now how about a real example?

Suppose that we want to implement the classic n! problem in Lisp. In the languages we know and love, this would look somewhat like this:

def factorial(n):

""" Return the factorial of n."""

if n <= 0: return 1

else: return n*factorial(n-1)

Now, let's take this definition and make it Lispy!

(defun factorial (n)

"Return the factorial of n"

(if (<= n 0)

1

(* n (factorial (- n 1)))))

Now, I guess I've got some 'splaining to do.

We'll take this chunk by chunk:

1. The defun macro basically tells your Lisp interpreter "Okay, interpreter, the next thing is going to be called a function called factorial. It will take as an argument a number n."

2. The "Returns the factorial of n" bit is a docstring, akin to what is in Python.

3. Now, we get to the bit of code that does the heavy lifting. This (if (<= n 0) part is a call to the if function. Its first argument is a call to the <= function, to see if n is <= 0. Note, however, that I did not close off the if list; if I did this, the forms that follow would not evaluate.

4. If the first condition, n <= 0 yields true, we simply return the atom 1 (atoms are essentially basic values in Lisp). This is the base case for our recursive call; 0! = 1 and we need to stop there, else we will recursively loop to infinity.

5. Now, we get to the last bit of code.

a. We'll start out by starting a call to the * function. Its first argument is the value we passed in originally, n.

b. Now, we make a call to the factorial function.

c. Then, we call the - function of n-1. Since n! is defined as n*(n-1)!, this is our recursive call.

6. And now, we close off the lists of function calls; in Lisp, it seems to be customary to put all the closing parenthesis on the last line.

And there you have it, an example of Lisp code. Not exactly revolutionary, but I think it might start an interesting discussion. To test the code and prove it, you'll need, naturally, a Lisp interpreter. As I am on Linux, I use SBCL, which I belive has been ported to Windows and Mac.

Happy hacking!