| |||||||||
Common Lisp, commonly abbreviated CL, is a dialect of Lisp, standardised by ANSI X3.226-1994. Developed to standardize the divergent variants of Lisp which predated it, it is not an implementation but rather a language specification to which most Lisp implementations conform.
Common Lisp is a general-purpose programming language, in contrast to Lisp variants such as Emacs Lisp and AutoLISP which are embedded extension languages in particular products. Unlike many earlier Lisps, but like Scheme, Common Lisp uses lexical scoping for variables.
Common Lisp is a multi-paradigm programming language that:
Common Lisp is a Lisp; it uses S-expressions to denote both code and data structure. Function and macro calls are written as lists, with the name of the function first, as in these examples:
Common Lisp has a plethora of data types, more than many languages.
Number types include integers, ratios, floating-point numbers, and complex numbers. Common Lisp uses bignums to represent numerical values of arbitrary size and precision. The ratio type represents fractions exactly, a facility not available in many languages. Common Lisp automatically coerces numeric values among these types as appropriate.
The Common Lisp character type is not limited to ASCII characters -- unsurprising, as Lisp predates ASCII. Some modern implementations allow Unicode characters.
The symbol type is common to Lisp languages, but largely unknown outside them. A symbol is a unique, named data object. Symbols in Lisp are similar to identifiers in other languages, in that they are used as variables to hold values; however, they are more general and can be used for themselves as well. Normally, when a symbol is evaluated, its value as a variable is returned. Exceptions exist: keyword symbols such as :foo evaluate to themselves, and Boolean values in Common Lisp are represented by the reserved symbols T and NIL.
Sequence types in Common Lisp include lists, vectors, bit-vectors, and strings.
As in any other Lisp, lists in Common Lisp are composed of conses, sometimes called cons cells or pairs. A cons is a data structure with two slots, called its car and cdr. A list is a linked chain of conses. Each cons's car refers to a member of the list (possibly another list). Each cons's cdr refers to the next cons -- except for the last cons, whose cdr refers to the nil value. Conses can also easily be used to implement trees and other complex data structures; though it is usually advised to use structure or class instances instead.
Common Lisp supports multidimensional arrays, and can dynamically resize arrays if required. Multidimensional arrays can be used for matrix mathematics. A vector is a one-dimensional array. Arrays can carry any type as members (even mixed types in the same array) or can be specialized to contain a specific type of members, as in a vector of integers. Many implementations can optimize array functions when the array used is type-specialized. Two type-specialized array types are standard: a string is a vector of characters, while a bit-vector is a vector of bits.
Hash tables store associations between data objects. Any object may be used as key or value. Hash tables, like arrays, are automatically resized as needed.
Packages are collections of symbols, used chiefly to separate the parts of a program into namespaces. A package may export some symbols, marking them as part of a public interface.
Structures, similar in use to C structs and Pascal records, represent arbitrary complex data structures with any number and type of fields (called slots).
In Common Lisp, the type of functions is a data type. For instance, it is possible to write functions that take other functions as arguments or return functions as well. This makes it possible to describe very general operations.
The Common Lisp library relies heavily on such higher-order functions. For example, the sort function takes a Scheme here, however. In CL, the function name is looked up in a namespace that is separate from the namespace for data variables; it is called the function namespace. Operators which define names in the function namespace include defun, flet, and labels.
To pass a function by name as an argument to another function, one must use the function special operator, commonly abbreviated as #'. The first sort example above refers to the function named by the symbol > in the function namespace, with the code #'>.
Scheme's evaluation model is simpler: there is only one namespace, and all positions in the form are evaluated (in any order) -- not just the arguments. Code written in one dialect is therefore sometimes confusing to programmers more experienced in the other. For instance, many CL programmers like to use descriptive variable names such as list or string which would be illegal in Scheme as they clash with function names.
Whether a separate namespace for functions is an advantage is a source of contention in the Lisp community. It is usually referred to as the Lisp-1 vs. Lisp-2 debate. These names were coined in a 1988 paper by filesystem. Because historically Lisp was separate from Unix, The Common Lisp pathname facility is more general than most operating systems' file naming conventions, making Lisp programs' access to files broadly portable across diverse systems.
Common Lisp also includes a toolkit for object-oriented programming, the Common Lisp Object System or CLOS.
A macro in Lisp superficially resembles a function in usage. However, rather than representing an expression which is evaluated, it represents a transformation of the program source code.
Macros allow Lisp programmers to create new syntactic forms in the language. For instance, this macro provides the until loop form, which may be familiar from languages such as Perl:
All macros must be expanded before the source code containing them can be evaluated or compiled normally. Macros can be considered functions that accept and return abstract syntax trees (Lisp S-expressions). These functions are invoked before the evaluator or compiler to produce the final source code. Macros are written in normal Common Lisp, and may use any Common Lisp (or third-party) operator available. The backquote notation used above is provided by Common Lisp specifically to simplify the common case of substitution into a code template.
Common Lisp macros are capable of variable capture, a situation in which symbols in the macro-expansion body coincide with those in the calling context. Variable capture is sometimes a desired effect: it allows the programmer to create macros wherein various symbols have special meaning. However, it can also introduce unexpected and unusual errors.
Some Lisp systems, such as Scheme, avoid variable capture by using macro syntax -- so-called "hygienic macros" -- that does not allow it. In Common Lisp, one can avoid unwanted capture by using Scheme -- if only because they are the two most popular Lisp dialects. Scheme antedates CL, and comes not only from the same Lisp tradition but from some of the same engineers -- Guy L. Steele, who with Gerald Jay Sussman designed Scheme, chaired the standards committee for Common Lisp.
Most of the Lisp systems whose designs contributed to Common Lisp -- such as Zetalisp and Franz Lisp -- used only dynamically-scoped variables. Scheme introduced lexically-scoped variables to Lisp, which were widely recognized as a good idea and adopted into CL. CL supports dynamically-scoped variables as well, but they must be explicitly declared as "special".
Common Lisp is sometimes termed a Lisp-2 and Scheme a Lisp-1, referring to CL's use of separate namespaces for functions and variables. (In fact, CL has many namespaces, such as those for go tags, block names, and loop keywords.) There is a long-standing controversy between CL and Scheme advocates over the tradeoffs involved in multiple namespaces. In Scheme, it is (broadly) necessary to avoid giving variables names which clash with functions; Scheme functions frequently have arguments named lis, lst, or lyst so as not to conflict with the system function list. However, in CL it is necessary to explicitly refer to the function namespace when passing a function as an argument -- which is also a common occurrence, as in the sort example above.
Common Lisp is defined by a specification (like Ada and C) rather than by a single implementation (like Perl). There are many implementations, and the standard spells out areas in which they may validly differ.
In addition, implementations tend to come with divergent sets of library packages, which provide functionality not covered in the standard. Some of these features have been rolled back into the standard, such as CLOS and the LOOP construct; others remain implementation-specific. Unfortunately, many valuable facilities for the modern programmer -- such as TCP/IP networking -- remain unstandardized. However, open-source packages have been created to support such features in a portable way, most notably the project.
Common Lisp has been designed to be implemented by incremental compilers. Standard declarations to optimize compilation (such as function inlining) are proposed in the language specification. Most Common Lisp implementations compile functions to native machine code. Others compile to bytecode, which reduces speed but eases binary-code portability. The misconception that Lisp is a purely-interpreted language is most likely due to the fact that Common Lisp environments provide an interactive prompt and that functions are compiled one-by-one, in an incremental way.
Some Unix-based implementations, such as CLISP, can be used as script interpreters; that is, invoked by the system transparently in the way that a Perl or Unix shell interpreter is.
Freely redistributable implementations include:
There are also non-free implementations available from , Xanalys, Digitool, Corman and Scieneer.
Despite the big expectations of the standard committee (CL was sometimes advertised as a possible replacement for C), Common Lisp remained a niche programming language, mostly used in academia and/or specific application areas often related to Artificial Intelligence.
However, there are famous success stories that at least show the big potential of the language in an industrial setting. To advertise their favorite language, many Common Lisp users take the example of the famous Yahoo! Store web-commerce site, originally developed in Common Lisp.
There also exist successful open-source applications written in Common Lisp, such as:
This non-exhaustive list illustrates the idea that Common Lisp is primarily used when (technically) difficult problems are adressed. Moreover, many CL applications implement "a language-in-the-language" approach, for which program-as-data manipulation as provided by the language (through s-expression representation and support for macros) is particularly adapted.