First Time Linux

Lisp

Lisp is an old language dating back several decades, perhaps best known for its popularity in the 1970s for use in artificial intelligence research, and its complete dissimilarity with other popular languages such as C++ or Fortran. It's renowned for the abundance of parentheses (or brackets) and takes a rather different approach to reading it than many other languages. It has often been observed that it's beneficial for a programmer to see completely alternative languages, completely alternative structures and thought processes, in order to better understand which tools are right for which types of problem.

The major concept behind lisp is that of lists. Everything is represented as a list. An array is obviously a list, so is a map, a single variable is a single-item list, a program method or function is also a list, and even the whole program itself is just a list of lists. Because there are so many techniques built in to handling lists, that means that functions and even whole programs can be processed, or rewritten, making for very powerful, self-modifying programs. That's one reason for its popularity in artificial intelligence circles, pattern recognition and to some extent graphics processing.

Getting and running

You can install GNU's implementation of so-called "common lisp" by looking for "clisp" in the package manager, or simply urpmi clisp from the comand line. Here on Mandriva that means clisp-2.46-1mdv2009.0 (note, the 2008.1 version is broken). After install, we can check the version number:

$ clisp --version
GNU CLISP 2.46 (2008-07-02) (built on n4.mandriva.com [192.168.2.53])

As with ruby and perl, just typing "clisp" brings you to an interactive mode (this one strangely with pro-Israel propaganda), where you can try out your first lisp expressions:

[1]> (+ 4 8)
12

This strange-looking expression is a list, surrounded by brackets(), with three items in the list, separated by spaces. The first thing in the list is a "+" sign, and this defines what operation to do on the other elements in the list. So it takes each of the other list elements (4 and 8) and adds them together, to give the final result 12. It may remind some readers of reverse polish notation on some calculators.

We can put longer lists in here, and combine lists within lists, as follows:

[2]> (- 10 7 1)
2
[3]> (+ 3 11 (- 6 3))
17

When you want to exit this interactive mode, don't use "quit" or "exit", but instead press Ctrl-D to return to the bash prompt. If you end up in a debugger, just keep pressing Ctrl-D until you come back to the surface.

Running a saved script

Now let's save our commands in a file, so we can run it any time we like. Use an editor (doesn't matter whether it's a graphical one like Kate or a console one like vi, use whatever you prefer) and create a new file called hello.lisp:

(print (+ 18 36 112 (* 12 11)))

After saving this file, we can give it straight to the interpreter:

$ clisp hello.lisp
298

This very simple example uses the print keyword to output to the screen, as otherwise the output would be lost inside the interpreter.

Making the file executable

Just like with other scripting languages, we can make our hello.lisp executable so we don't have to type clisp <file> to run it, but just <file>. As before, edit the file to include the following line as the first line of the file:

#! /usr/bin/clisp

Next we need to give ourselves execute permission for this file:

$ chmod u+x hello.lisp

and then we can execute it directly:

$ ./hello.lisp

More info

Obviously we've only had chance to scratch the bare surface of the language here, but it's an illustration of how to get the basics working on Mandriva. From here you can follow any lisp documentation and go into more advanced topics.

A good place to start is Wikipedia's Lisp page for some basic background, or the wikibook on Lisp programming which is still unfortunately rather incomplete. One of the great free resources is Peter Seibel's Practical common Lisp.