Mar de Mármara

Common Lisp is an impure functional language of the family Lisp.

Quotation

“Common Lisp, the force of a clay ball, is the quite alive patriarch of the family Lisp.

The Engineer gave him form, and not the Researcher in search of conceptual clearness. But in spite of its practical applicability, CL offers pleasures alas unknown of the programming mainstream . ”

CAT of Recursion

Introduction

Common Lisp is a dialect of Lisp standardized by the ANSI X3.226-1994. Developed to standardize the divergent alternatives of Lisp which preceded it, it is not an implementation but a specification to which the Lisp implementations try to conform. It is frequently shortened in CL .

Common Lisp is a computer programming language of general use, a contrario of dialects of Lisp like Emacs Lisp and AutoLisp, which is languages of extension embarked in particular products. Contrary to many Lisp older, but like Design, Common Lisp the Portée lexical by defect for the variables uses.

Common Lisp is a Computer programming language multi-paradigms which:

  • Accepts techniques of imperative Programmation, functional and directed object (CLOSED).

  • Is dynamically typified, but with optional declarations of the type which can improve the effectiveness and safety,
  • Dispose of a powerful Management system of exceptions, named Condition System (management system of conditions),
  • Is syntactically extensible through functionalities like the Macro S and the macros of reading.

Syntax

Like any language of the Lisp family, Common Lisp uses S-expression S to indicate at the same time the code and certain structures of data. The invocations of functions, special forms and macros are written in the literal syntax of the lists, with the name of the function (resp. special, and macro form) in first place, as in these examples: (+ 2 2); adds 2 and 2, returns 4 (setf E 2.7182817); assign 2.7182817 with the variable E (defun squares (X) (* X X)) ; a function defines which puts a number squared (squares 3); execution of the function: turn over 9

Types of data

Common Lisp has a plethora of the types of data, more than any other language.

Scalar types

Numbers

The numerical types include the rational whole S, , the numbers with Floating decimal point and the complex numbers. Common Lisp uses great numbers to represent numerical values of arbitrary size and precision. The rational type represents the fractions in an exact way. Common Lisp automatically converts the numerical values between these types of suitable way. Here the numerical turn , i.e. the hierarchy of the numerical types of Common Lisp:

complex ratio fixnum /// number <--+-real--+-rational--+-integer--+-bignum \ \ \ (1) signed-byte--unsigned-byte--bit \ float--+-shorts-float \ - individual-float \ - double-float \ - length-float

(1) integer and signed-byte are disjoined specifications of the types; however, the fields are identical.

As example, the expression (+ (sqrt -2) (/6 4)) turn over

#C (3/2 1.4142135)

i.e. a complex number of which the imaginary part is floating it 1.4142135 and the real part is rational the 3/2.

Characters

The type Common Lisp character is not limited to the characters ASCII; that is not surprising because Lisp is older than the ASCII. Certain modern implementations support the characters Unicode.

Symbols

The type Symbole is common to the Lisp languages, but largely unknown in-outside. A symbol is a named, single object, relative to a Espace of names. The letter symbols in Lisp are used as identifiers of variables; however, they are more generals and can be used for themselves also, like objects. When a symbol is evaluated, its value as a variable is turned over.

The existence of the symbols as a type of data facilitates the writing of the macros (which carry out transformation of code at the compile time) and the implementation of systems of calculation symbolic system. In fact, Lisp was the first language of implementation of systems of formal calculation. Very complete systems of formal calculation were written in Lisp (Maxima and Axiom, and for the transformations symbolic systems, they generally support the comparison with popular the Mathematica and Maple).

There exist particular cases:

  • the Boolean values are represented by the reserved symbols T and the NILE.

  • the keywords (of the form: foo or: bar) are symbols defined in the space of name KEYWORD . The evalutation of such a symbol turns over the symbol itself.

Examples: foo; ; - > variable FOO does not have a value. (function foo); ; - > function FOO is not defined. Operator QUOTE protects the symbols from the evaluation (when one wants to use a symbol for itself): (quote foo); ; - > FOO 'foo; ; - > FOO One can ask whether a symbol is related to a value or a function: (boundp 'foo); ; - > the NILE (not of dependant value) (fboundp 'foo); ; - > the NILE (no named function FOO exists) Association symbol-value: (defparameter foo 77); ; - > FOO foo; ; - > 77 Association symbol-function: (defun foo (bar) (1+ bar)) ; ; - > FOO Call of function FOO with value FOO (the fact illustrates that a symbol has slots separate for the values and the functions): (foo foo); ; - > 78

(boundp 'foo); ; - > T (fboundp 'foo); ; - > T (function foo); ; - > #

Structures of data

Sequences

The sequences are an abstract type representing an ordered collection of elements. The concrete types derived from sequence are the lists and the vectors (including the vectors of bits and character strings). Many functions are available for the sequences. It is not however possible to the programmer to define his own sub-types of sequences. An extension of the standard allowing the definition of new sequences is in the course of proposal (circa 2007).

Pairs, Lists

The lists of Common Lisp are not a type of data but made up starting from conses (plural), are sometimes called cells even idiots or . Idiots is a structure with two elements of the type T, accessible by the functions because and cdr (or first and rest ). A list is thus a chain of conses bound, where the cdr of each idiots points on the following element, and the last cdr point on the NILE value. The conses can be easily used to implement trees or any structures of data complex; although in this last case it is recommended to use structures or classes.

The tree (1 (2/7 3.14) has " foo") is represented by the following chain of IDIOTS:

It can be built various ways, we quote two of them: (list 1 (list 2/7 3.14) 'has " foo") (idiots 1 (idiots (idiots 2/7 (idiots 3.14 the NILE)) (idiots 'has (idiots " foo" The NILE))))

The lists or cyclic structures made starting from pairs do not have a literal representation, but they can be printed: (setf *print-circle* T); activate the posting of the circular structures (avoids infinite looping) (let ((list (Copy-list '(B has c)))) (setf (cdr (last list)) list)) ; => #1= (HAS B C. #1#)

Tables

Common Lisp supports the arbitrary tables of dimensions, and dynamically can also redimensionner the tables. Multidimensional tables can be used for mathematics of the matrices. Only the tables with a dimension (named vectors) are a sub-type of sequence.

The tables can be specialized by the type of the elements which they contain. In particular, the vectors of bits and the vectors of characters (chains) are provided out of standard by the language.

Example of creation of a three-dimensional board (4 X 2 X 3) and initialized: (make-array '(4 2 3): initial-content '(((B has c) (1 2 3)) ((D E F) (3 1 2)) ((G H I) (2 3 1)) ((J K L) (0 0 0))))

… that turns over:

#3A (((B HAS C) (1 2 3)) ((D E F) (3 1 2)) ((G H I) (2 3 1)) ((J K L) (0 0 0)))

Tables of chopping

The tables of chopping store associations between objects. Any type of object can be used as key or value. The tables of chopping, like the tables, are automatically redimensionnées if necessary.

Parcellings

Parcellings ( packages ) are collections of symbols, used mainly for partitionner a program in spaces of names. A parcelling can export certain symbols, marking them like part of a public interface. The variables and methods known as private of the traditional object-oriented languages (principle of the encapsulation) are obtained in Lisp by declaring them in a space of name, without exporting them.

Structures

The Structures , similar to the structs of C and the records (recordings) of the Pascal, represent structures of data of arbitrary complexity, with an unspecified number and any type of fields (called slots ). The structures support the limited shape of heritage. For the needs for the directed programming object, one will refer to CLOSED.

Classes and Objects

Common Lisp was the first standardized object-oriented language (in 1995, by ANSI). The part of the treating language of the objects names CLOSED for Common Lisp Object System . The projecting characteristics of FIELD are the following ones:

  • it is a system with classes (there exist indeed systems with prototypes),

  • it allows the multiple Héritage between classes,
  • the classes themselves are objects, or authorities of Méta-class S (of the classes of classes),
  • he has a protocol with méta-objects (Meta Object Protocol or MOP), related to the existence of the méta-classes, and allowing to modify the semantics and the behavior of the system,
  • he offers the multiple Sélection method S, i.e. the selection with the execution of a method according to the type of the tuple of its obligatory parameters (and not of a receiver privileged as in the languages with simple Sélection, which are the very great majority); the methods of FIELD are gathered in generic functions instead of belonging to classes,
  • it allows the Combinaison of methods, i.e. the definition of auxiliary methods being carried out before and/or after a special method.

CLOSED also allows to define méta-classes and classes, to change the class of an object, with the execution .

The Système of conditions of Common Lisp uses CLOSED to define the types of the conditions being able to occur with the execution.

Certain implementations propose in extension of CLOSED a Protocole to méta-objects described by the book The Art off the Metaobject Protocol .

Lexical functions and closings

Functions

In Common Lisp, the functions are a type of data. For example, it is possible to write functions which take other functions in parameter, and turn over functions (they are named functions of a higher nature , or of first class ). That makes possible to write very general operators.

Example

For example, the function leaves (tri) takes a sequence and an operator of comparison in parameter. It can be used not only to sort any type of data, but also for sorting structures of data according to a key. ((list 5 2 6 3 1 4) # leaves ' >); ; - > (6 5 4 3 2 1), by using the function > as operator of comparison (`leaves ((9 a) (3 b) (4 c)) (lambda (X there) (< (first X) (first there)))) ; ; - > ((3 b) (4 c) (9 a)), i.e the list sorted on the first element

One can apply function FOO defined higher in a sequence: (mapcar # ' foo (list 1 2 3 4 5)) ; ; - > (2 3 4 5 6)

Spaces of names

Common Lisp respectively has a space of name for the functions and the variables (with the difference of, for example, Scheme, which is known as " Lisp-1"). Lisp-2 (or more) has the advantage which no name of variable can mask a name of function: one can name a variable idiots or even yew without problem. However, to refer to a function as a variable, one must use the function (function…) or equivalent notation # ' as in the examples above.

In addition to the functions and the variables, there are a space of names distinct for the couples from operators block/return-from and tagbody/go.

Let us add to finish that the space of name of the functions is actually shared between the functions themselves and the various kinds of macro.

Evaluation

The model of evaluation is simple: when the appraiser meets an expression (F A1 A2… An), the symbol F can represent one of these items:

  1. a special operator (like yew ),

  2. macro,
  3. a function, i.e. the name of a function defined by (defun…) or an anonymous function, always indicated by (lambda…)

If F is a function, the parameters are evaluated successively from left to right and the function is called upon with the computed values of the parameters. For the special operators or the macros, that depends . These operators indeed tend to control the evaluation of their parameters. For example, the operator yew does not evaluate all his parameters, it must evaluate his condition and then according to the result, a branch of the aternative one.

Lexical capture

A lexical closing is a function whose free variables capture the connections of the lexical environment in which they are defined. That makes it possible to build functions having an internal state (out of C one would use the keyword static to obtain the internal state, but the lexical capture is not possible). One can build simple objects starting from closings, for example a factory of meters: (defun manufacture-meter (); manufacture-meter returns a function which increments and posts its internal value (let ((value 0)) ; in the environment of the factory, one creates the value of the meter (lambda (); the new meter itself (incf value)))) ; here, the reference to " valeur" capture its definition in the factory

Other types

The other types of data of Common Lisp include/understand:
  • the Pathnames (names representing of the ways) which represent files and repertories in the Filing system. The tool of naming of ways in Common Lisp is more general than the majority of the convention of naming of the operating systems, which returns the access of largely portable the Lisp programs to the files through various systems.

  • the streams (floods) of entry and exit represent sources and wells of binary and textual data, like the terminal or of the open files.
  • Common Lisp has its own generator of pseudo-random numbers. The objects random State represent reusable sources of pseudo-random numbers, making it possible the user to initialize the generator or to force it with rejouer a sequence.
  • the Conditions are a special type used to represent “interesting” errors, exceptions and other events to which a program must be able to answer. Common Lisp with the one of the the most complete Management system of exceptions; it allows the recovery after error.

Common Lisp also incorporates a toolbox for the directed Programmation object, the Common Lisp Object System, or CLOSED. It is thus possible to add an infinity of the types.

Macros

A Macro in Lisp resembles a function superficially. The macros make it possible to the Lisp programmer to create new syntactic forms in the language, by transformation of their parameters representing of the source code. For example, the macro following one provides the form of loop until (to buckle… until), which is familiar in a language like Perl.
  • definition of macro:

(defmacro until (test &rest body) `(C () (, test) , @body))
  • call the macro one (use in a program):

(until (= (random 10) 0) (Write-line " Hello"))
  • first stage of macro-expansion (allows to find the text of the definition interpolated with the parameters of the call):

(C THE NILE ((= (RANDOM 10) 0)) (WRITE-LINE " Hello"))
  • complete macro-expansion (what is carried out for a call):

(NILE BLOCK (LET THE NILE (TAGBODY #: G7594 (IF (= (RANDOM 10) 0) (GO #: G7595)) (WRITE-LINE " hello") (PSETQ) (GO #: G7594) #: G7595 (RETURN-FROM the NILE (PROGN)))))

The macros are not thus not evaluated with the execution like the functions, but at the compile time. The value of return of macro is its macro-expansion , i.e. the transformation of code which it carried out; it is that which is evaluated during the execution of the program for each call to macro.

One can consider the macros as functions which accept and turn over abstract trees of syntax (S-expressions), but control the evaluation of their parameters. Indeed in a call to macro (my-macro (+ 1 2)), the expression (+ 1 2) is not initially not evaluated and its result passed in argument, it passed such as it is to macro, which can re-use it intact, interpolating it with other fragments of code or transforming it more or less completely.

Like the functions, the macros can use the whole of the language Common Lisp (and libraries third) to carry out their alteration (one calls them for that of the procedural macros ), contrary to the macros of the language C which allow only substitutions of character strings the level of the source, without access to the whole of the language C itself.

Embarked languages

The principal interest of the macros does not lie in the small utilities like the example above, whose proliferation in the Lisp programs can lead to an effect of obfuscation, but in the possibility of defining languages embarked in Lisp, which target a particular applicatif field. A traditional example is inclusion in the language of an extension making it possible to make logical programming with the manner of Prolog or of the programming by constraint (addition of new paradigm of programming). It is possible indeed to build, by using macros and functions Lisp, a compiler for a language moreover high level which the basic language but which remains integrated in this last.

All the applicatifs fields can profit from the technique consisting in writing a program by having a downward approach ( signal-down ), in which the problem to be solved is broken up until being able to be expressed in the terms of the basic language, and a rising approach ( bottom-up ), where one extends the basic language with concepts facilitating the expression of the problem. No computer programming language of general use which cannot be provided with the operators specific to an infinity of specific scopes of application, the ability to be able to build an adapted language (the approach bottom-up ), by extension and re-use of the base, is a major advantage of Lisp on the other universal languages . The macros are from this point of view a single advantage of the languages of the Lisp family. The facility of writing of the macros is due to the use of the S-expression S for the syntax of Lisp.

Templates for the code

The mechanism of the macros would not be convenient enough with the use without a mechanism making it possible to represent Lisp code in the form of owner (model, or template ) in whom elements calculated or given in parameter can be injected. Common Lisp offers the quasiquotation , represented by the character `(known as backquote ). Although the operator backquote can be used in functions, it is in the macros that its use proves to be paramount: it makes it possible to improve the legibility of the code produced by macro in considerable proportions.

In the example above, the backquotes were used to calculate the result. The body macro until is entirely represented by a template of code, using the operator of iterations C . Let us recall that C accepts three arguments: a whole of variables of iterations defined locally and reiterated in parallel, a whole of clauses allowing to define the conditions of iteration of the variables and stop of the loop, and a group - of unspecified size - arbitrary operations or actions: C ({VAr | (VAr [[step-form])}*) (end-test-form result-form*) declaration* {tag | statement} * => result*

For the loop until, the use of C is simple: there are no variables of iteration, there is a clause of test exactly, and one can admit a group of operation to be carried out with each iteration. The template to be produced reflects that: `(C () (, test) , @body))

One notes the backquote preceding the expression C. Into a backquotée expression, one can inject information in two manners: with the operator , ( unquote ) which injects the object immediately on its line, or the operator , @ ( splice-unquote ) which admits a list on its line and on the spot injects the contained of the list.

The result of: (macroexpand-1 '(until (= (random 10) 0) (Write-line " Hello")))

is the expansion (C THE NILE ((= (RANDOM 10) 0)) (WRITE-LINE " Hello")) in which one finds the template combined with the parameters of the call to macro. It will be noted that the final expansion, produced by (macroexpand…), contains the expansion of the operator C itself in terms of operators moreover low level. As C is an operator defined by the language Common Lisp, the semantics of the operator is always the same one. But it may be that this expansion varies from one compiler to another. One sees by there that the macros are already massively used in the implementation of a rich and complex language like Common Lisp.

Capture of variable

The macros Common Lisp are capable of captures of variable , a situation where symbols located in the body of the macro-expansion coincide with symbols of the appealing context. For this reason they are sometimes called macro “nonhygienic”, by comparison with the macro system the “hygienic ones” of Scheme, which guarantees separation between this whole of symbols.

The capture of variable is sometimes a desired effect; when it is not the case, it must be avoided by the use of Gensym S, or symbols whose unicity is guaranteed.

Implementations

Standard and implementations

Common Lisp is defined by a specification (like Ada and C) rather than by only one implementation (like Perl or Python). There are many implementations, and the standard describes the points on which they can be divergent for good reasons.

Moreover, the implementations are generally provided with various whole of “packages” of libraries, which provide functionalities not - glazes by the standard. Some of these functionalities were introduced thereafter into the standard, as CLOSED and LOOP forms it; others remain specific to these implementations. Many conveniences for the modern programmer -- like the access to the networks TCP/IP -- remain out of the standard, but are provided by the various implementations with sometimes minor differences. A process named CLRFI (Common Lisp Request For Improvement), similar to the SRFI of Design, aims to make converge the useful functionalities left out of standard ANSI of 1995.

According to a spread error, the implementations of Common Lisp are all of the interpreters. In fact, compilation belongs to the specification of the language . The majority of the implementations of Common Lisp compile the functions towards computer code. Others compile towards Object code or even towards the code of a applicative Machine virtual, which reduces speed but improves the portability.

Certain implementations in environment UNIX, like CLISP, can be used like interpreters of scripts (or even like Shell).

List implementations

The implementations freely redistribuables include:

  • CMU Common Lisp (CMUCL), of Carnegie Mellon University. CMUCL is perhaps the CL most largely used among the free software. 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, an implementation compiling of the code-object. It is portable and turns on many UNIX and systems of the UNIX type, just as on Microsoft Windows and several other systems.
  • Steel Bank Common Lisp (SBCL), a branch of CMUCL. “Approximately, SBCL is distinguished from CMU CL by a greater insistence on maintainability. ”. On Linux 2.6, SBCL support the threads native. SBCL turns on the same platforms as CMUCL, less HP/UX, and more Linux for PowerPC, SPARC, and MIPS.
  • GNU Common Lisp (GCL), the Lisp compiler of the project GNU. Not yet completely conforms to standard ANSI, GCL is however the implementation of choice for several large projects, including mathematical tools Maxima and ACL2. GCL turns on GNU/Linux on eleven various architectures, and also under Windows, Solaris, and FreeBSD.
  • Embeddable Common Lisp (ECLS), conceived to be embarked in applications written in C.
  • OpenMCL, a branch Open Source of Macintosh Common Lisp. As its name indicates it, OpenMCL is native on Macintosh; it is carried out on Mac OS X, Darwin, and Linux for PowerPC.
  • Armed Bear Common Lisp, is an implementation, under development, of a compiler CommonLisp → virtual machine Java. ABCL is provided with an editor to do everything, J, is written in Common Lisp.

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

See too

Applications

Common Lisp is used successfully in many commercial applications:

  • Orbitz, a major site of reservation of voyages,
  • Mirai, a continuation integrated of creation of graphics 2D/3D offering what is almost universally regarded as the best modeller polygonal of industry, a system of animation IK/FK and non-linear (popularized by products such as Animanium of Sega and Softimage SXI), and of coloring advanced for the 2D and the 3D. It is used in various films (most famous being the Lord of the Rings of New Line Cinema), of the video games and military simulations.
  • Piano, a packetage for the preliminary design of commercial aircraft and the evaluation of the competition.
  • Xanalys Corp. offer a line of software of investigation, used by the police services, of safety and prevention of the fraud on a world level.
  • Knowledge International Technologies mechanical software of design ICAD.
  • International Genworks the Declaratory Language of General use (GDL), development tools for the creation of engineering Web, design and applications of businesses.

There exist also open-source applications written in Common Lisp, such as:

  • Applicative Common Lisp, a complete demonstrator of theorems for a subset of Common Lisp.
  • Maximum
  • , a system of sophisticated formal calculus.
  • Axiom, one (other) system of sophisticated formal calculus.
  • Compo, a language allowing the natural description of complex musical structures.
  • LISA, a system containing rules to build software agents " intelligents".
  • OpenMusic an environment of visual Programming conceived for the musical Composition, but giving an almost complete access, in graphic form, with the programming in Common Lisp and CLOSED.

Common Lisp is also used by many governments and institutions of the type Loi 1901. Examples of use by NASA include:

  • SPIKE, the planning system and scheduling of the Space telescope Hubble.

  • Remote Agent, gaining software price of NASA for the year 1999.

External bonds

  • the aggregator Planet Lisp
  • common-lisp.net Lodgings of many projects
  • Common Lisp HyperSpec, a hypertextuelle version of the standard.
  • CLiki, Wiki Common Lisp.
  • The Association off Lisp Users.
  • The Common Lisp Cookbook, a collection of useful methods of programming.
  • the site of Paul Graham, a developer and lawyer of the use of Common Lisp; its site contains many tests on Lisp and the programming in general.
  • a French forum on Lisp and Common Lisp

Random links:Stephen Hopkins | Battle of Almenara | Sukagawa | End-of-life of a boat | Whims festival | Mer_de_Marmara