|
|
Common LISP#REDIRECT Common Lisp Common LispCommon Lisp, commonly abbreviated CL (not to be confused with Combinatory logic which is also abbreviated CL), is a dialect of Lisp programming language, 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 programming language and AutoLISP which are embedded extension languages in particular products. Unlike many earlier Lisps, but like Scheme programming language, Common Lisp uses lexical scoping for variables. Common Lisp is a multi-paradigm programming language that: * Supports programming techniques such as procedural programming, functional programming 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 Lisp macros (compile-time code rearrangement accomplished by the program itself) and reader macros (extension of syntax to give special meaning to characters reserved for users for this purpose). ==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 (setf p 3.1415) ; sets the variable "p" 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 (computing)'' type is not limited to ASCII characters -- unsurprising, as Lisp predates ASCII. Some modern implementations allow Unicode characters. [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 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. ===Data structures=== ''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 programming language structs and Pascal programming language records, represent arbitrary complex data structures with any number and type of fields (called ''slots''). Class ''instances'', similar to structures, but created by the object system, CLOS. ===Functions=== 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 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 (list 5 2 6 3 1 4) #'>)
; Sorts the list using the > function as the comparison operator.
; Returns (6 5 4 3 2 1).
(sort (list '(9 a) '(3 b) '(4 c))
#'(lambda (x y) (< (car x) (car y))))
; Sorts the list according to the first element (car) of each sub-list.
; Returns ((3 b) (4 c) (9 a)).
The evaluation model for functions is very simple. When the evaluator encounters a form (F A1 A2...) then it is to assume that the symbol named F is one of the following:
# A special operator (easily checked against a fixed list)
# A macro operator (must have been defined previously)
# The name of a function (default), which may either be a symbol, or a sub-form beginning with the symbol lambda.
If F is the name of a function, then the arguments A1, A2, ..., An are evaluated in left-to-right order, and the function is found and invoked with those values supplied as parameters.
====Defining functions====
Named functions are defined using the special operator defun. A function definition must give the name of the function, the names of any arguments, and a function body:
(defun square (x)
(* x x))
Function definitions may include ''declarations'', which provide hints to the compiler about optimization settings or the data types of arguments. They may also include ''documentation strings'' (docstrings), which the Lisp system may use to provide interactive documentation:
(defun square (x)
(declare (number x) (optimize (speed 3) (debug 0) (safety 1)))
"Calculates the square of the number x."
(* x x))
Anonymous functions are defined using the lambda special operator. Lisp programming style frequently uses higher-order functions for which it is useful to provide anonymous functions as arguments.
There are a number of other operators related to the definition and manipulation of functions. For instance, a function may be recompiled with the compile operator. (Some Lisp systems run functions in an interpreter by default unless instructed to compile; others compile every entered function on the fly.)
====The function namespace====
The namespace for function names is separate from the namespace for data variables. This is a key difference between Common Lisp and Scheme programming language. 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 programming language'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 could cause problems in Scheme as they would locally shadow 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 Richard P. Gabriel, which extensively compares the two approaches. [http://www.nhplace.com/kent/Papers/Technical-Issues.html]
===Other types===
Other data types in Common Lisp include:
*''Pathnames'' represent files and directories in the 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.
*Input and output ''streams'' represent sources and sinks of binary or textual data, such as the terminal or open files.
*Common Lisp has a built-in pseudo-random number generator. ''Random state'' objects represent reusable sources of pseudo-random numbers, allowing the user to seed the PRNG or cause it to replay a sequence.
*''Conditions'' are a special type used to represent errors, exceptions, and other "interesting" events to which a program may respond.
*''Classes'' are first-class objects, and are themselves instances of classes called metaclasses.
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 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:
(defmacro until (test &body body)
`(do ()
(,test)
,@body))
;; example
(until (= (random 10) 0)
(write-line "Hello"))
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.
====Variable capture and shadowing====
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 gensyms: guaranteed-unique symbols which can be used in a macro-expansion without threat of capture.
Another issue is the inadvertant ''shadowing of operators'' used in a macroexpansion. For example, consider the following (incorrect) code:
(macrolet ((do (...) ... something else ...))
(until (= (random 10) 0) (write-line "Hello")))
The UNTIL macro will expand into a form which calls DO which is intended to refer to the built-in special form DO. However, in this context, DO may have a completely different meaning.
Common Lisp ameliorates the problem of operator shadowing by forbidding the redefinition of built-in operators, such as DO in this example. Moreover, users may separate their own code into ''packages''. Built-in symbols are found in the COMMON-LISP package, which will not be shadowed by a symbol in a user package.
==Comparison with other Lisps==
Common Lisp is most frequently compared with, and contrasted to, Scheme programming language—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 dynamic scoping variables in their interpreters and lexical scoping variables in their compilers. Scheme introduced the sole use of lexically-scoped variables to Lisp (an inspiration from ALGOL_68), which was 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". There are no differences in scoping between ANSI CL interpreters and compilers.
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.
CL also differs from Scheme in its handling of boolean values. Scheme uses the special values #t and #f to represent truth and falsity. CL follows the older Lisp convention of using the symbols T and NIL, with NIL standing also for the empty list. In CL, ''any'' non-NIL value is treated as true by conditionals such as if. This allows some operators to serve both as predicates (answering a boolean-valued question) and as returning a useful value for further computation.
Lastly, the Scheme standards documents require tail recursion, which the CL standard does not. Most CL implementations do offer tail-call optimization, although often only when the programmer uses an optimization directive. Nonetheless, common CL coding style does not favor the ubiquitous use of recursion that Scheme style prefers -- what a Scheme programmer would express with tail recursion, a CL user would usually express with an iterative expression in do, dolist, loop, or (more recently) with the iterate package.
==Implementations==
Common Lisp is defined by a specification (like Ada programming language and C programming language) rather than by a single implementation (like Perl programming language). There are many implementations, and the standard spells out areas in which they may validly differ.
In addition, implementations tend to come with library packages, which provide functionality not covered in the standard. Free Software libraries have been created to support such features in a portable way, most notably the [http://clocc.sourceforge.net/ Common Lisp Open Code Collection] 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.
===List of implementations===
Freely redistributable implementations include:
* CMUCL, originally from Carnegie Mellon University, now maintained as Free Software by a group of volunteers. CMUCL uses a fast native-code compiler. It is available on Linux and Berkeley Software Distribution for Intel x86; Linux for Alpha; and Solaris, IRIX, and HP-UX on their native platforms.
* [http://clisp.sourceforge.net/ GNU CLISP], a bytecode-compiling implementation. It is portable and runs on a number of Unix and Unix-like systems (including Mac OS X), as well as Microsoft Windows and several other systems.
* [http://sbcl.sourceforge.net/ Steel Bank Common Lisp] (SBCL), a branch from CMUCL. "Broadly speaking, SBCL is distinguished from CMU CL by a greater emphasis on maintainability." [http://sbcl.sourceforge.net/cmucl-relationship.php] SBCL runs on the platforms CMUCL does, except HP/UX; in addition, it runs on Linux for PowerPC, SPARC, and MIPS, and on Mac OS X. SBCL does not use an interpreter; all expressions are compiled to native code.
* [http://www.gnu.org/software/gcl/gcl.html GNU Common Lisp] (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 (ECL), designed to be embedded in C programming language applications;
* [http://openmcl.clozure.com/ OpenMCL], 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.
* Movitz implements a Lisp environment for x86 computers without relying on any underlying OS.
* [http://www.cliki.net/Armed%20Bear%20Lisp Armed Bear Common Lisp] Armed Bear Lisp is a Common Lisp implementation that runs on the Java Virtual Machine. It includes a compiler to Java programming language byte codes, and allows access to Java libraries from Common Lisp.
* [http://jatha.sourceforge.net/ Jatha] is a Java library that implements a fairly large subset of Common LISP
Commercial implementations are available from [http://www.franz.com Franz, Inc.], [http://www.lispworks.com/ Xanalys Corp.], [http://www.digitool.com/ Digitool, Inc.], [http://www.cormanlisp.com/ Corman Technologies] and [http://www.scieneer.com/index.html Scieneer Pty Ltd.].
==Applications==
Common Lisp is used in many successful commercial applications, the most famous (no doubt due to Paul Graham's promotion) being the Yahoo! Store web-commerce site. Other notable examples include:
*[http://www.orbitz.com Orbitz], a major travel booking site powered by Lisp.
*[http://www.izware.com/mirai/index.htm Mirai], [http://www.izware.com/ Izware LLC]'s fully integrated 2d/3d computer graphics content creation suite that features what is almost universally regarded as the best polygonal modeler in the industry, an advanced IK/FK and non-linear animation system (later popularized by such products as Sega's Animanium and Softimage XSI, respectively), and advanced 2d and 3d painting. It is used in major motion pictures (most famously in New Line Cinema's Lord of the Rings), video games and military simulations.
*[http://www.xanalys.com/ Xanalys Corp.]'s line of investigation software, used by police, security and fraud prevention services worldwide.
*[http://www.ktiworld.com/ Knowledge Technologies International]'s ICAD mechanical design software, one of the leading products in the field.
There also exist successful open-source applications written in Common Lisp, such as:
* [http://www.cs.utexas.edu/users/moore/acl2/ Applicative Common Lisp], a full-featured theorem prover for a subset of Common Lisp.
* [http://maxima.sourceforge.net/ Maxima], a sophisticated computer algebra system.
* [http://compo.sourceforge.net Compo], a language allowing complex musical structures to be described in a natural way.
* [http://lisa.sourceforge.net Lisa], a production-rule system to build "intelligent" software agents.
As well, Common Lisp is used by many government and non-profit institutions. Examples of its use in NASA include:
*[http://www.stsci.edu/resources/software_hardware/spike/ SPIKE], the Hubble Space Telescope planning and scheduling system.
*[http://ic.arc.nasa.gov/projects/remote-agent/ Remote Agent], winner of the 1999 NASA Software of the Year Award.
==External links==
* The [http://www.lisp.org/HyperSpec/FrontMatter/index.html Common Lisp HyperSpec], a hypertext version of the Common Lisp standard.
* The [http://ww.telent.net/cliki/index CLiki], a Wiki for Free Software Common Lisp systems running on Unix-like systems.
* [http://www.lisp.org/ The Association of Lisp Users].
* [http://www.unmutual.info/startingwithcl.html A quick guide to starting with Common Lisp].
* [http://www.cs.cmu.edu/Web/Groups/AI/html/cltl/cltl2.html Common Lisp the Language, 2nd Edition], known as "CLtL2". Guy Steele's book on Common Lisp, which served as the basis for the ANSI Common Lisp standard.
* [http://cl-cookbook.sourceforge.net/ The Common Lisp Cookbook], a collection of useful programming methods.
* [http://www.paulgraham.com/lisp.html Paul Graham's page] about Lisp. If you follow the links you can find his book ''On Lisp'' online, which is concerned with macro design in Common Lisp.
* [http://www-2.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/LispBook/index.html Common Lisp: A Gentle Introduction to Symbolic Computation] by David S. Touretzky, available online and aimed at beginners.
* [http://www.norvig.com/ Peter Norvig's page] containing many interesting resources about Common Lisp.
* [http://www.gigamonkeys.com/book/ Practical Common Lisp] Online version of book of same name.
* [http://www.lisperati.com/casting.html Casting Spells in Lisp] A cartoon introduction to Common Lisp.
LISP dialects
LISP programming language family
Functional languages
Major programming languages
Object-oriented programming languages
Common Lisp== Kyoto CL == I think that the 'See Also' link to Kyoto Common Lisp is irrelevant and should be flushed. KCL is now only of historical interest since it does not conform to the ANSI standard. Its successors, among them GNU Common Lisp (GCL) are out there, but frankly I don't think that there should even be entries for them, or even perhaps for WCL which is also listed. If nobody complains in a few days then I'll remove those links. --User:James Crippen (Fri Mar 26 08:48:54 UTC 2004) :It is not the way of Wikipedia to discard knowledge about historically relevant software simply because it is not currently useful software. You can see this, for instance, in the number of articles we have for implementations of BASIC programming language. The current article on Kyoto Common Lisp claims that it conforms to the ANSI standard; if this is inaccurate (it should probably at most say "purports to conform") it should certainly be corrected. But please do not delete entries for this or other software simply because nobody uses it anymore -- "historical interest" is not a reason to delete, but a capital reason to ''retain'' Wikipedia articles. --User:Fubar Obfusco 14:14, 26 Mar 2004 (UTC) ::I edited the page a little bit and forgot to summarize (BTW: this was the first time I edited a wikipedia page). First, I removed the "perhaps the most" in "CMUCL is perhaps the most widely-used [...] implementation". It is a subjective opinion about something that is perhaps not even a fact (gcl and clisp are both very popular, and SBCL seems to gain popularity too). I also added two entries in the external links. --User:Fredokun (Mon Apr 12 10:11:01 UTC 2004) == Dynamic scoping in pre-Common Lisps == It is not true that Scheme introduced static scoping, and it is very unlikely to be true that ZetaLisp was dynamically scoped. Before Scheme, you had two behaviors; an implementation's interpreter usually used dynamically scoped variables, while its compiler used statically scoped variables. This is the case in Lisp 1.5 (I have the manual which documents that), MACLisp, and very probably ZetaLisp (though I haven't tried in this last one). Since ZetaLisp was basically MACLisp with some additions, and both have greatly contributed to the design of Common Lisp, the statement "Most of the Lisp systems whose designs contributed to Common Lisp [...] used only dynamically-scoped variables" is incorrect. I have therefore corrected the article. --User:Zsam 01:48, 17 Feb 2005 (UTC) == Sentence meaning == ''Common Lisp automatically coerces numeric values among these types as appropriate.'' What does that mean? --User:Marudubshinki 21:34, 12 May 2005 (UTC) :You don't have to cast numbers into the appropriate type before operating on them. The result will automatically be of a type that respects the operands' precision and the result's value. :You can add an integer and a float and get the right answer: (+ 4.5 4) is the float 9.5. If you add the ratios 1/5 and 4/5, the answer is the integer 1, not a ratio. Likewise, if you add the complex numbers #C(0 -1) and #C(1 1) -- that is, and -- the answer is integer 1, not a complex. --User:Fubar Obfusco 02:11, 13 May 2005 (UTC) :: As a follow-up -- here's the definition of ''coerce'' from the CLHS glossary: ::: coerce ''v.t.'' (an object to a type) to produce an object from the given object, without modifying that object, by following some set of coercion rules that must be specifically stated for any context in which this term is used. The resulting object is necessarily of the indicated type, except when that type is a subtype of type complex; in that case, if a complex rational with an imaginary part of zero would result, the result is a rational rather than a complex---see Section 12.1.5.3 (Rule of Canonical Representation for Complex Rationals). [http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_c.htm] :: Not sure if that helps, but there it is. --User:Fubar Obfusco 16:02, 13 May 2005 (UTC) :::Your first one cleared it up, thanks. The second one... not much so. --User:Marudubshinki 16:13, 13 May 2005 (UTC) == How do I define a function == The article contains a paragraph which tells us how to use a macro but it does not tell how to create a function. *You do it with defun, as explained in the Syntax section. Or you can use lambda expression as explained in the main LISP article. Grue ">User:Grue 18:12, 12 Jun 2005 (UTC) Common lisp#REDIRECT Common Lisp See other meanings of words starting from letter: CCA | CB | CD | CE | CF | CG | CH | CI | CJ | CK | CL | CM | CN | CO | CP | CR | CS | CT | CU | CW | CX | CY | CZ |Words begining with Common_Lisp: Common_LISP Common_Lisp Common_Lisp Common_lisp Common_Lisp_programming_language
Sponsored links: praca, nurkowanie.
|
These materials are based on Wikipedia and licensed under the GNU FDL
YouTube.com videos better site than Turbo Tax 2007 |
|
|