Invented in 1958, Lisp is the second oldest computer programming language, and it has spawned several modern derivatives, including Common Lisp, elisp, Clojure, Racket, Scheme, Fennel, and GNU Guile. People who love thinking about the design of programming languages often love Lisp because of how its syntax and its data share the same structure: Lisp code is essentially a list of lists, and in fact its name is an acronym for *LISt Processing*. People who love thinking about the aesthetics of programming languages often hate Lisp because of its frequent use of parentheses for scoping; in fact, it's a common joke that Lisp actually stands for *Lots of Irritating Superfluous Parentheses*. Whether you love or hate its design philosophies, Lisp is an interesting glimpse at the past and, thanks to Clojure and Guile, also into the future. You might be surprised how much Lisp code there is lurking within big code bases in any given industry, so it's a good idea to have at least a passing familiarity with the language.

Installing Lisp {#_installing_lisp}

There are many implementations of Lisp. Popular open source versions include

SBCL

and

GNU Common Lisp (GCL)

. You can install either of these with your distribution's package manager. For instance, on Fedora Linux:

On Debian:

For Mac, you can use

MacPorts

or

Homebrew

.

For Windows, download a binary from

gnu.org/software/gcl

.

This article uses `gcl` and its command `clisp`, but most principles in this article apply to any Lisp.

List processing {#_list_processing}

The basic unit of Lisp source code is an *expression*, which is written as a list. For instance, this is a list of an operator (`+`) and two integers (`1` and `2`):

It's also a Lisp expression, providing a symbol (`+`) which evaluates to a function (addition) and two arguments (`1` and `2`). You can run this expression and others in an interactive Common Lisp environment called REPL (read-eval-print loop). If you're familiar with Python's IDLE, then the Lisp's REPL should feel somewhat familiar to you. To launch a REPL, launch Common Lisp:

At the REPL prompt, type a few expressions:

Functions {#_functions}

Now that you know the basic structure of a Lisp expression, you can utilize Lisp functions in a useful way. The `print` function displays any argument you provide it to your terminal, while the `pprint` function "pretty" prints it. There are other variations on the print function, but `pprint` is nice in REPL:

You can create your own functions with `defun`. The `defun` function requires a name for your function and any parameters you want your function to accept.

Variables {#_variables}

You can create variables in Lisp with `setf`:

You can nest expressions within expressions, in a kind of pipeline. For instance, you can pretty-print the contents of your variable after invoking the `string-upcase` function to convert its characters to uppercase:

Lisp is dynamically typed in the sense that you don't have to declare variable types when setting them. For instance, Lisp treats integers as integers by default:

Similarly, it treats a string as a one-dimensional array of characters.

You can do some introspection on objects using the `typep` function, which tests for a specific data type.

The single quote (`'`) before `string` and `integer` prevents Lisp from (incorrectly) evaluating those keywords as variables.

It's a shorthand way to protect the terms, normally done with the `quote` function:

Lists {#_lists}

Unsurprisingly, you can also create lists in Lisp:

Lists can be indexed with the `nth` function:

Scripting with Lisp {#_scripting_with_lisp}

Lisp can be compiled, or it can be used as an interpreted scripting language. The latter is probably the easiest option when just starting out, especially if you're already familiar with Python or

shell scripting

.

Here's a simple dice roller script written in GNU Common Lisp:

The first line tells your

POSIX

terminal what executable to use to run the script.

The `roller` function, created with `defun`, uses the `random` function to print a pseudo-random number up to, and not including, the zeroth item of the `num` list. The `num` list hasn't been created yet in the script, but the function doesn't get executed until it's called.

The next line assigns any argument provided to the script at launch time to a variable called `userput`. The `userput` variable is a list, and it's what becomes `num` once it's passed to the `roller` function.

The penultimate line of the script starts a *random seed*. This provides Lisp with enough entropy to generate a mostly random number.

The final line invokes the custom `roller` function, providing the `userput` list as its sole argument.

Save the file as `dice.lisp` and mark it executable;

Finally, try running it, providing it with a maximum number from which to choose its random number:

Not bad!

Learning Lisp {#_learning_lisp}

Lisp is a fun and unique language, with an ever-growing developer base and enough historic and emerging dialects to keep programmers from all disciplines happy. Whether you can imagine using Lisp as a utilitarian language for personal scripts, or to advance your career, or to just as a fun experiment, you can see some particularly inventive uses for it at the annual

Lisp Game Jam

. You have a few months until the next one, so there's plenty of time to practice!

Proxied content from gemini://sdf.org/klaatu/geminifiles/lisp.gmi (external content)

Gemini request details:

Original URL
gemini://sdf.org/klaatu/geminifiles/lisp.gmi
Status code
Success
Meta
text/gemini
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.