Common Lisp has many
data types.
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. Most 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 with several parts: name, value, function, property list, and package. Of these,
value cell and
function cell are the most important. Symbols in Lisp are often used similarly to identifiers in other languages: to hold the value of a variable; however there are many other uses. Normally, when a symbol is evaluated, its value is returned. Some symbols evaluate to themselves, for example, all symbols in the keyword package are self-evaluating. Boolean values in Common Lisp are represented by the self-evaluating symbols T and NIL. Common Lisp has namespaces for symbols, called 'packages'. A number of functions are available for
rounding scalar numeric values in various ways. The function round rounds the argument to the nearest integer, with halfway cases rounded to the even integer. The functions truncate, floor, and ceiling round towards zero, down, or up respectively. All these functions return the discarded fractional part as a secondary value. For example, (floor -2.5) yields −3, 0.5; (ceiling -2.5) yields −2, −0.5; (round 2.5) yields 2, 0.5; and (round 3.5) yields 4, −0.5.
Data structures Sequence types in Common Lisp include lists, vectors, bit-vectors, and strings. There are many operations that can work on any sequence type. As in almost all other Lisp dialects,
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 or the empty list. 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 in a list, 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. It is also possible to create circular data structures with conses. Common Lisp supports multidimensional
arrays, and can dynamically resize
adjustable 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 bits. Usually, only a few types are supported. 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 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. Packages can use other packages.
Structures, similar in use to
C structs and
Pascal records, represent arbitrary complex data structures with any number and type of fields (called
slots). Structures allow single-inheritance.
Classes are similar to structures, but offer more dynamic features and multiple-inheritance. (See
CLOS). Classes have been added late to Common Lisp and there is some conceptual overlap with structures. Objects created of classes are called
Instances. A special case is Generic Functions. Generic Functions are both functions and instances.
Functions Common Lisp supports
first-class functions. 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
relational operator as an argument and key function as an optional keyword argument. This can be used not only to sort any type of data, but also to sort data structures according to a key. ;; Sorts the list using the > and ) ; Returns (6 5 4 3 2 1) (sort (list 5 2 6 3 1 4) #' ;; Sorts the list according to the first element of each sub-list. (sort (list '(9 A) '(3 B) '(4 C)) #' The evaluation model for functions is very simple. When the evaluator encounters a form (f a1 a2...) then it presumes 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 The
macro defun defines functions where a function definition gives the name of the function, the names of any arguments, and a function body: (defun square (x) (* x x)) Function definitions may include compiler
directives, known as
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) "Calculates the square of the single-float x." (declare (single-float x) (optimize (speed 3) (debug 0) (safety 1))) (the single-float (* x x))) Anonymous functions (
function literals) are defined using lambda expressions, e.g. (lambda (x) (* x x)) for a function that squares its argument. Lisp programming style frequently uses higher-order functions for which it is useful to provide anonymous functions as arguments. Local functions can be defined with flet and labels. (flet ((square (x) (* x x))) (square 3)) There are several other operators related to the definition and manipulation of functions. For instance, a function may be compiled with the compile operator. (Some Lisp systems run functions using an interpreter by default unless instructed to compile; others compile every function).
Defining generic functions and methods The macro defgeneric defines
generic functions. Generic functions are a collection of
methods. The macro defmethod defines methods. Methods can specialize their parameters over CLOS
standard classes,
system classes,
structure classes or individual objects. For many types, there are corresponding
system classes. When a generic function is called,
multiple-dispatch will determine the effective method to use. (defgeneric add (a b)) (defmethod add ((a number) (b number)) (+ a b)) (defmethod add ((a vector) (b number)) (map 'vector (lambda (n) (+ n b)) a)) (defmethod add ((a vector) (b vector)) (map 'vector #'+ a b)) (defmethod add ((a string) (b string)) (concatenate 'string a b)) (add 2 3) ; returns 5 (add #(1 2 3 4) 7) ; returns #(8 9 10 11) (add #(1 2 3 4) #(4 3 2 1)) ; returns #(5 5 5 5) (add "COMMON " "LISP") ; returns "COMMON LISP" Generic Functions are also a
first class data type. There are many more features to Generic Functions and Methods than described above.
The function namespace A key difference between Common Lisp and
Scheme is that Common Lisp's namespace for function names is separate from its namespace for data variables. For Common Lisp, operators that define names in the function namespace include defun, flet, labels, defmethod and defgeneric. This follows the pattern established by John McCarthy's
Lisp 1.5, which first split variables and functions into their own namespaces; John McCarthy's original Lisp language having only a single namespace for both.
Multiple return values Common Lisp supports the concept of
multiple values, where any expression always has a single
primary value, but it might also have any number of
secondary values, which might be received and inspected by interested callers. This concept is distinct from returning a list value, as the secondary values are fully optional, and passed via a dedicated side channel. This means that callers may remain entirely unaware of the secondary values being there if they have no need for them, and it makes it convenient to use the mechanism for communicating information that is sometimes useful, but not always necessary. For example, • The TRUNCATE function rounds the given number to an
integer towards zero. However, it also returns a remainder as a secondary value, making it very easy to determine what value was truncated. It also supports an optional divisor parameter, which can be used to perform
Euclidean division trivially: (let ((x 1266778) (y 458)) (multiple-value-bind (quotient remainder) (truncate x y) (format nil "~A divided by ~A is ~A remainder ~A" x y quotient remainder))) ;;;; => "1266778 divided by 458 is 2765 remainder 408" • GETHASH returns the value of a key in an
associative map, or the default value otherwise, and a secondary Boolean indicating whether the value was found. Thus code that does not care about whether the value was found or provided as the default can simply use it as-is, but when such distinction is important, it might inspect the secondary Boolean and react appropriately. Both use cases are supported by the same call and neither is unnecessarily burdened or constrained by the other. Having this feature at the language level removes the need to check for the existence of the key or compare it to
null as would be done in other languages. (defun get-answer (library) (gethash 'answer library 42)) (defun the-answer-1 (library) (format nil "The answer is ~A" (get-answer library))) ;;;; Returns "The answer is 42" if ANSWER not present in LIBRARY (defun the-answer-2 (library) (multiple-value-bind (answer sure-p) (get-answer library) (if (not sure-p) "I don't know" (format nil "The answer is ~A" answer)))) ;;;; Returns "I don't know" if ANSWER not present in LIBRARY Multiple values are supported by a handful of standard forms, most common of which are the MULTIPLE-VALUE-BIND special form for accessing secondary values and VALUES for returning multiple values: (defun magic-eight-ball () "Return an outlook prediction, with the probability as a secondary value" (values "Outlook good" (random 1.0))) ;;;; => "Outlook good" ;;;; => 0.3187
Other types Other data types in Common Lisp include: •
Pathnames represent files and directories in the
filesystem. 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 (PRNG).
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 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
metaobject classes (
metaclasses for short). •
Readtables are a type of object which control how Common Lisp's reader parses the text of source code. By controlling which readtable is in use when code is read in, the programmer can change or extend the language's syntax. ==Scope==