Encyclopedia > Common Lisp

  Article Content

Common Lisp

Common Lisp 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:

  • Supports programming techniques such as imperative, functional and object-oriented programming.
  • Is dynamically typed, but with optional type declarations that can improve efficiency or safety.
  • Is extensible through standard features such as macros and reader macros.

Table of contents

Syntax 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:

 (+ 2 2)           ; adds 2 and 2, yielding 4
 (setq pi 3.1415)  ; sets the variable "pi" equal to 3.1415

 ; Define a function that squares a number
 (defun square (x) (* x x))
 ; Execute the function
 (square 3)        ; Returns "9"

Data types

Common Lisp has a plethora of data types, more than many languages.

Scalar types

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. [1] (http://www.cliki.net/Unicode%20Support)

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 can be used as variables to hold values; however, they are more general and can be used for themselves as well. Boolean values in Common Lisp are represented by the reserved symbols T and NIL.

Data structures

Sequence types in Common Lisp include arrays, vectors, bit-vectors, and strings. Common Lisp supports multidimensional arrays, and can dynamically resize arrays as needed. Multidimensional arrays can be used for matrix mathematics.

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 of two elements, called its car and cdr. A list is a linked chain of conses wherein each cons's cdr points to the next element, and the last cdr points to the NIL value. Conses can also easily be used to implement trees and other complex data structures.

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.

Conditions are a special type used to represent errors, exceptions, and other "interesting" events to which a program may respond.

Functions

In Common Lisp, functions are a data type. For instance, it is possible to write functions that take other functions as arguments. This makes it possible to make very general operations.

For instance, the sort function takes a comparison operator as an argument. This can be used not only to sort any type of data, but also to sort data structures according to a key.

 (sort '(5 2 6 3 1 4) #'>)
 ; Returns (6 5 4 3 2 1), using the > function as the comparison operator

 (sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))
 ; Returns ((3 b) (4 c) (9 a)), i.e. the list sorted by the first element

Common Lisp is a Lisp-2, meaning that there are separate namespaces for defined functions and for variables. (This differs from, for instance, Scheme, which is a Lisp-1.) Lisp-2 has the advantage that a local variable name will never shadow a function name: One can call a variable cons or even if with no problems. However, to refer to a function variable one must use the #' notation, as in the above examples.

Other types

Common Lisp also has data types to represent packages, file pathnames, input and output streams, sources of random numbers, and tructures (comparable to C structs or Pascal records).

Common Lisp also includes a toolkit for object-oriented programming, the Common Lisp Object System or CLOS.

Macros

A macro in Lisp superficially resembles a function. However, rather than representing an expression which is evaluated, it represents a transformation of the program text within the macro call.

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:

 (defmacro until (test &rest body)
   `(do ()
        (,test)
      ,@body))

This differs from a function in that it can repeatedly evaluate its arguments. A function's arguments are evaluated only once, before the function is called; a macro controls its arguments' evaluation or other use in the macro-expansion.

Variable capture

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. For this reason, they are sometimes called "unhygienic" macros, in contrast to Scheme's "hygienic" macros, which enforce separation between these sets of symbols. (The terminology strikes some Common Lisp programmers as needlessly insulting.)

Variable capture is sometimes a desired effect; when it is not, it must be avoided using gensyms[?], or guaranteed-unique symbols.

Implementations 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.

It is a common misconception that Common Lisp implementations are all interpreters. In fact, compilation is part of the language specification. Most Common Lisp implementations compile functions to native machine code. Others compile to bytecode, which reduces speed but improves portability.

Some Unix-based implementations, such as CLISP, can be used as script interpreters.

List of implementations

Freely redistributable implementations include:

  • CMU Common Lisp (http://www.cons.org/cmucl/) (CMUCL), from Carnegie Mellon University. CMUCL is perhaps the most widely-used open source CL. It is available on Linux and BSD for Intel x86; Linux for Alpha; and Solaris, IRIX, and HP-UX on their native platforms.
  • GNU CLISP (http://clisp.sourceforge.net/), a bytecode-compiling implementation. It is portable and runs on a number of Unix and Unix-like systems, as well as Microsoft Windows and several other systems.
  • Steel Bank Common Lisp (http://sbcl.sourceforge.net/) (SBCL), a branch from CMUCL. "Broadly speaking, SBCL is distinguished from CMU CL by a greater emphasis on maintainability." [2] (http://sbcl.sourceforge.net/cmucl-relationship.php) SBCL runs on the platforms CMUCL does, except HP/UX, and also on Linux for PowerPC, SPARC, and MIPS.
  • GNU Common Lisp (http://www.gnu.org/software/gcl/gcl) (GCL), the GNU Project's Lisp compiler. Not yet fully ANSI-compliant, GCL is however the implementation of choice for several large projects including the mathematical tools Maxima and ACL2. GCL runs under GNU/Linux on eleven different architectures, and also under Windows, Solaris, and FreeBSD.
  • Embeddable Common Lisp (http://ecls.sourceforge.net/) (ECLS), designed to be embedded in C applications;
  • OpenMCL (http://openmcl.clozure.com/), an open source branch of Macintosh Common Lisp. As the name implies, OpenMCL is native to the Macintosh; it runs on Mac OS X, Darwin, and Linux for PowerPC.

There are also commercial implementations available from Franz, Xanalys, Digitool, Corman and Scieneer.

External links

see also: WCL, Kyoto Common Lisp.



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Quioque, New York

... are 65 years of age or older. The median age is 39 years. For every 100 females there are 113.3 males. For every 100 females age 18 and over, there are 108.7 males. The ...

 
 
 
This page was created in 27.9 ms