Lisp (which stands for "LISt Processing") is a programming language oriented towards functional programming. Its prominent features include prefix-notation syntax, dynamic typing (variables are type-neutral, but values have implicit type), and the ability to treat source code as first-class objects.
Not counting the various machine languages and assembly languages, Lisp is the second-oldest programming language still in widespread use; only Fortran is older. Like Fortran, it has changed greatly since its early days.
Strictly speaking, Lisp is now not a single language but a family of similarly-styled languages with an instantly recognizable appearance. These are known as Lisp dialects; the most well-known are Common Lisp and Scheme.
|
Lisp was originally implementated on an IBM 704 computer, and two instructions on that machine became the primitive operations for decomposing lists: car
(Contents of Address Register) and cdr
(Contents of Decrement Register). Most dialects of LISP still use car
and cdr
for the operations that returning the first item in a list and the rest of the list respectively.
Because of its expressiveness and flexibility, Lisp became popular with the artificial intelligence community. However, Lisp had its downsides as well: programs generate a large amount of intermediate output, which take up memory and have to be garbage collected. This made it difficult to run Lisp on the memory-limited stock hardware of the day. In the 1970s, an increasing user community and generous government funding led to the creation of LISP machines: dedicated hardware for running Lisp environments and programs.
During the 1980s and 1990s, a great effort was made to unify the numerous Lisp dialects into a single language. The new language, Common Lisp, was essentially a superset of the dialects it replaced. In 1994, ANSI published the Common Lisp standard, "ANSI X3.226-1994 Information Technology Programming Language Common Lisp." By this time the world market for Lisp was much smaller than in its heyday.
The language is amongst the oldest programming languages still in use as of the time of writing in 2003. Algol, Fortran and COBOL are of a similar vintage, and Fortran and COBOL are also still being used.
McCarthy's 1958 paper introduced two types of syntax: S-expressions (Symbolic Expressions), which are also called sexp's, and M-expressions[?] (Meta Expressions), which express functions of S-expressions. M-expressions never found favour, and almost all LISPs today use S-expressions to manipulate both code and data.
The heavy use of parentheses in S-expressions has been criticized -- one joke acronym for Lisp is "Lots of Irritating Superfluous Parentheses" -- but the S-expression syntax is also responsible for much of Lisp's power: the syntax is extremely regular, which facilitates manipulation by computer.
The reliance on expressions gives the language great flexibility. Because Lisp functions are themselves written as lists, they can be processed exactly like data: programs can easily be written to manipulate other programs. This is known as metaprogramming[?]. Many Lisp dialects exploit this feature using macro systems, which make it possible to extend the language almost without limit.
A Lisp list is written with its elements separated by whitespace, and delimited by parentheses. For example,
(1 2 "foo")
is a list whose elements have the values 1
, 2
, and "foo"
. These values are implicitly typed: they are respectively two integers and a string, and do not have to be declared as such. The empty list ()
is also represented as nil
.
Expressions are written as lists, using prefix notation[?]. The first element in the list is the name of a form, i.e. a function, operator, macro, or "special form" (see below.) The remainder of the list are the arguments. For example, the function list
returns its arguments as a list, so the expression
(list 1 2 "foo")
evaluates to the list (1 2 "foo")
. If any of the arguments are expression, they are recursively evaluated before the enclosing expression is evaluated. For example,
(list 1 2 (list 3 4))
evaluates to the list (1 2 (3 4))
. Note that the third argument is a list; lists can be nested.
Arithmetic operators are treated similarly. The expression
(+ 1 2 3 4)
evaluates to 10. Note that this is much more compact (if less familiar) than "1+2+3+4
", which is the equivalent under infix notation.
"Special forms" provide LISP's control structure. For example, the special form if
takes three arguments. If the first argument is non-nil, it evaluates to the second argument; otherwise, it evaluates to the third argument. Thus, the expression
(if nil (list 1 2 "foo") (list 3 4 "bar"))
evaluates to (3 4 "bar")
. (Of course, this would be more useful if a non-trivial expression had been substituted in place of nil
!)
Another special form, defun
, is used to define functions. The arguments to defun
are a list of arguments and the expression that the function evaluates to.
A minimal Lisp needs just a few functions implemented in an underlying language (such as machine language, or C on Unix systems):
All the other functions may be implemented in terms of these -- albeit not very efficiently. Actual Lisp systems implement a much larger set of functions than this.
As the reader may have noticed from the above discussion, Lisp syntax lends itself naturally to recursion. Mathematical problems such as the enumeration of recursively-defined sets are simple to express in this notation. This function evaluates to the factorial of its argument:
(defun factorial (n) (if (<= n 1) 1 (* n (factorial (- n 1)))))
This is an alternative function, which is more efficient in most Lisp systems because it uses tail recursion:
(defun factorial (n &optional (acc 1)) (if (<= n 1) acc (factorial (- n 1) (* acc n))))
This function takes a list argument and evaluates to the reverse of the list. (Lisp actually has a built-in reverse function which does the same thing.)
(defun reverse (l &optional acc) (if (atom l) acc (reverse (cdr l) (cons (car l) acc))))
CLOS features multiple inheritance, multiple dispatch ("multimethods"), and a powerful system of "method combinations". In fact, Common Lisp, which includes CLOS, was the first object-oriented language to be officially standardized.
Differences between dialects may be quite significant -- for instance, Common Lisp and Scheme do not even use the same keyword to define functions! Within a dialect that is standardized, however, conformant implementations support the same core language, but with different extensions and libraries.
Note: The following list is a mix of dialects and implementations, and is not in chronological order!
wikipedia.org dumped 2003-03-17 with terodump