View the Most Wanted LQ Wiki articles.
LinuxQuestions.org > Linux Wiki > Lisp

From LQWiki

Jump to: navigation, search

Lisp (List processing) is a programming language that was invented by John McCarthy in 1958 and is commonly used in artificial intelligence. Some joke that because of its syntax, the name stands for "Lots of irritating single parentheses". Lisp programs were often interpreted rather than compiled, allowing convenience when used for interactive configuration like in emacs. However most modern lisp environments compile to native code, either directly or by using C as a portable assembler and using the systems C compiler.

LISP is AI's mother tongue, a language based on the ideas of (a) variable-length lists and trees as fundamental data types, and (b) the interpretation of code as data and vice-versa. Invented at MIT in the late 1950s, it is actually older than any other HLL still in use except FORTRAN. Accordingly, it has undergone considerable adaptive radiation over the years; modern variants are quite different in detail from the original LISP 1.5. The dominant HLL among hackers until the early 1980s, LISP has since shared the throne with C. Its partisans claim it is the only language that is truly beautiful.

All LISP functions and programs are expressions that return values; this, together with the high memory utilization of LISPs, gave rise to Alan Perlis's famous quip (itself a take on an Oscar Wilde quote) that “LISP programmers know the value of everything and the cost of nothing”.

One significant application for LISP has been as a proof by example that most newer languages, such as COBOL and Ada, are full of unnecessary crocks. When the right thing has already been done once, there is no justification for bogosity in newer languages.

Contents

Basic syntax

In lisp, everything is a function call wrapped in parentheses, even basic arithmetic.

(+ 2 2)        ; Yields 4

(The semicolon begins a comment.) Though the syntax is annoying at first, it's easy to get used to. It is more consistent than most other programming languages, and easier (for a computer) to interpret. The familiar if-then-else clause looks like this:

(if (> x 0) x (- x))        ; Returns the absolute value of x

The "list" part of the name comes about because the linked list is a fundamental data type in lisp. In fact, every lisp expression is a list whose first element is the function to call. To retreive the first element of a list, use the car function.

(car '(1 2 3))      ; Returns 1

The cdr function returns the rest of the list, much like the pointer to the second element in a linked list would point to the rest of the list. There are also some functions to combine car and cdr.

(cdr '(1 2 3))      ; Returns (2 3)
(cadr '(1 2 3))     ; Returns 2

The ANSI Common Lisp standard actually defines several of these concatenated constructions as shortcuts to recursion, allowing such things as:

(cdddr  '(1 2 3 4 5)) ; Returns (4 5)
(cadddr '(1 2 3 4 5)) ; Returns 4

This (admittedly strange) functionality is not found in any other language.

Lisp Interpreters

The most widely distributed lisp interpreter is built into GNU emacs. If you don't specify a file, emacs starts in lisp-interaction-mode. In this mode, you can enter lisp expressions and press Ctrl-J to evaluate them. To evaluate a single lisp expression in any editing mode, press ESC then ':'. Unfortunately, Emacs does not support ANSI Common Lisp.

The main freely available Common Lisp interpreters are the CLISP (which is capable of bootstrapping from C) which compiles to byte-code and Armed Bear Common Lisp that is implemented in java and compiles to JVM bytecode.

Lisp Compilers

Most modern Lisp environments are either only compilers or have a compiler besides an interpreter. Major commercial systems are Allegro Common Lisp, Corman Lisp, Lispworks and Scieneer Lisp. Opensource environments include CMUCL (from Carnegie Mellon), SBCL (Steel Bank Common Lisp) and openmcl that compile directly to native code and ECL and GCL that convert to C code that they them compile and load.

Attribution

This article is based, in whole or in part, on entry or entries in the Jargon File.

See also


Personal tools