NETB131 Programming Project
Programming language Common Lisp
An overview

Dimitar Ivanov Savov, F40164


Navigation

  1. History and special features
  2. "Hello World!" Program and comment opertator ";"
  3. Fundamental data types and Assignment operator
    1. Variable enviroments
    2. Assignment
    3. Sequences
  4. Basic Control Flow (conditional and loop statements)
    1. Conditional operators
    2. Loop operators
  5. Functions
    1. Constructors
    2. Operators
    3. Mapping functions
    4. Utility functions
    5. Input/Output
  6. Arrays
    1. Constructors
    2. Selectors
  7. Compilers
  8. Projects and Software in Common Lisp
  9. External links
  10. Tutorials

1. History and Special Features


Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard X3.226-1994. Developed to standardize the divergent variants of Lisp which predated it, it is not an implementation but rather a language specification. Several implementations of the Common Lisp standard are available, including commercial products and open source software

2. "Hello World" Program and comment operator ";"


(print "Hello World!") ;;Hello World!
Note: Common Lisp ignores the comment operator (;) and everything after it on the line

3. Fundamental data dypes and Assignment operator


Common Lisp has many data types, more than many other languages.

3.1Variable Environments


(let ( {symbol | (symbol form)}*) special form
[(declare (special variables))]
forms)
Establishes each symbol as the name of a variable. The scope of each of these variables is limited to the let form, unless it is named in the declaration, in which case that variable has dynamic scope. If a form is given with a symbol, the value of the form becomes the initial value of the variable named by the symbol. These forms are evaluated in order, but the assignment of their values to their variables is done in parallel. A variable without an initialization form is initialized to NIL. Variables named in the declaration that are not established by this let form are special variables established elsewhere that will be used in the forms of this let. After the establishment and initialization of all the variables, the forms are evaluated in order, and the value of the last one becomes the value of the let form.
(let* ( {symbol | (symbol form)}*) special form
[(declare (special variables))]
forms)

Exactly like let except that the assigning of initial values to the variables is done in order so that one variable may be used in the initialization form of a later variable.

3.2Assignment


(psetf {place form}*) ;;macro
Just like setf, except evaluates all the forms first, then assigns the values to the corresponding places in parallel. Returns NIL.
(psetq {symbol form}*) ;;macro

Just like setq, except evaluates all the forms first, then assigns the values to the corresponding variables in parallel. Returns NIL.
(set symbol form) ;;function

Binds the special (dynamic) variable named symbol to the value of form.
(setf {place form}*) ;;macro

Evaluates the first form and stores its value in the generalized variable specified by the first place, then goes on to the next place form pair, and so on. Returns the value of the last form.
(setq {symbol form}*) ;;special form

Evaluates the first form and stores its value in the variable named by the first symbol (which is not evaluated), then goes on to the next symbol form pair, and so on. Returns the value of the last form.

3.3Sequences


(prog1 forms) ;;macro
The forms are evaluated in order and the value of the first is returned.
(prog2 forms) ;;macro

The forms are evaluated in order and the value of the second is returned.
(progn [forms]) ;;special form

The forms are evaluated in order and the value of the last is returned.

3.4Exits

(return [form]) ;;macro

form is evaluated and exit is made from the lexically innermost dolist, dotimes, or loop form. The value of form is returned by that form. If form is omitted, NIL is returned.


4. Basic Control Flow (conditional and loop statements)

4.1Conditional operators

(case form ;;macro
((objects1) forms1)
...
((objectsn) formsn))

form is evaluated. If its value is among the objectsi, then the formsi are evaluated and the value of the last formsi is returned as the value of the call to case. The objectsi are not evaluated. If the value of form is not among any of the objectsi, NIL is returned unless either t or otherwise appears instead of (objectsn), in which case the formsn are evaluated and the value of the last formsn is returned. If objectsi consists of only one object, that object may appear without the surrounding parentheses as long as it is not nil, t, otherwise, or a cons. It is an error for one object to appear among more than one objectsi.
(cond (test1 [forms1]) ... (testn [formsn])) ;;special form

Evaluates the testi in order until one of them, testj say, evaluates to True. Then evaluates formsj and returns the value of the last one. If there are no formsj, the value of testj is returned. If no testi evaluates to True, NIL is returned.
(if test then [else]) ;;special form
If test evaluates to True, evaluates and returns the value of then; otherwise, if else is present, evaluates and returns the value of else; otherwise, returns NIL.
(typecase form (type1 forms1) ... (typen formsn)) ;;macro
form is evaluated. formsi are evaluated for the lowest i for which the value of form is of type typei and the value of the last formsi is returned as the value of the call to typecase. The typei are not evaluated. If the value of form is not any of the typei, NIL is returned unless either t or otherwise appears instead of (typen), in which case the formsn are evaluated and the value of the last formsn is returned.
(unless test forms) ;;macro
Evaluates test. If the value is NIL, evaluates the forms in order and returns the value of the last one. Otherwise, does not evaluate the forms and returns NIL.
(when test forms) ;;macro
Evaluates test. If the value is NIL, does not evaluate the forms and returns NIL. Otherwise, evaluates the forms in order and returns the value of the last one.

4.2Loop operators

In these three macros, a statement is a non-atomic (that is, a list) form.
(dolist (symbol list-form [result-form]) statements) ;;macro

Evaluates statements repeatedly with symbol successively bound to the members of the value of list-form (which must evaluate to a list). Then evaluates result-form, and returns that value. If result-form is omitted, dolist returns NIL. Premature exit may be made with an explicit return form.
(dotimes (symbol form resultform) statements) ;;macro

Evaluates statements repeatedly with symbol successively bound to the integers zero, 1, and so on, up to, but not including, the value of form, which should evaluate to an integer. Then resultform is evaluated and its value is returned. If form evaluates to zero or to a negative integer, the statements are not evaluated at all. Premature exit may be made with an explicit return form.
(loop statements) ;;macro

The statements are evaluated in order repeatedly forever. The only way to exit a loop is to evaluate a return that is lexically within the loop form.

5. Functions



In common lisp functions are divided into types as follows :

5.1Constructors


(function fn) ;;special form
If fn is a symbol, returns the function named by that symbol. If fn is a lambda expression, returns the functional closure of that expression. An alternate printed representation of (function fn) is #’fn.

5.2Operators


(apply function [forms1] ([forms2])) ;;function
A list of forms1 is appended to the list of forms2, and the function is applied to that list of argument forms. If a symbol is given instead of a function, the function the symbol names is used.
(funcall function [forms]) function
Applies the function to the argument forms. function may only be a function; it can’t be a macro or a special form.

5.3Mapping Functions


(mapc function lists) ;;function

There must be the same number of lists as the number of arguments function takes. function is applied to the first member of each of the lists, the second member, and so on, until the shortest list is exhausted. The first of the lists is returned.
(mapcan function lists) ;;function
function must return a list, and there must be the same number of lists as the number of arguments function takes. function is applied to the first member of each of the lists, the second member, and so on, until the shortest list is exhausted. The nconc of the values is returned.
(mapcar function lists) ;;function
There must be the same number of lists as the number of arguments function takes. function is applied to the first member of each of the lists, the second member, and so on, until the shortest list is exhausted. A list of the values is returned.

5.4Utility Functions


(assert assertion [(variables1) [string variables2]]) ;;macro
If the form assertion evaluates to True, returns NIL. Otherwise, an error is forced and Lisp prints an error message and gives the user the choice of aborting the computation or replacing the current values of the variables listed as variables1. If the user chooses to replace the current values, the assert form is retried with the new values. The error message is formed, in part, by applying format to string and variables2.
(check-type variable type-specifier) ;;macro

If the variable is of the type specified by the type-specifier, returns NIL. Otherwise, an error is forced and Lisp prints an error message and gives the user the choice of aborting the computation or replacing the current value of variable. If the user chooses the latter, the check-type form is retried with the new value.
(deftype symbol () doc-string ’(satisfies function)) ;;macro

Defines symbol to be the name of Common Lisp type consisting of all those objects that satisfy the predicate function, which must be a function of one argument. doc-string is retrievable from symbol by documentation. The form (satisfies function) may be replaced by any type-specifier that might be used in check-type (see above), in which case the type named by symbol is the type specified by typespecifier.
(time form) ;;macro
Evaluates form and prints how long it took to do the evaluation. Returns the value of form.
(trace [function-names]) ;;macro
Turns on tracing of the specified functions (function-names are not evaluated.) With no arguments, returns a list of all functions being traced.
(untrace [function-names]) ;;macro
Turns off tracing of the specified functions (function-names are not evaluated.) With no arguments, turns off all tracing.

5.5Input/Output


(format stream control-string arguments) ;;function

Produces a printed representation of its arguments, as specified by the control-string. If stream is nil, this printed representation is returned as a string. If stream is t, the printed representation is printed to the standard output file (usually the terminal). control-string is a string of characters that get printed as they appear in the string, intermixed with format directives. Some format directives consume arguments and specify how these arguments are printed. Other format directives do not consume arguments, but control the printing in other ways. Nonconsuming directives include

~% Causes a newline to be printed. ~& Causes a newline to be printed unless printing is already at the beginning of a new line ~~ Causes the character ~ to be printed. ~#\newline A ~ appearing in the control-string right before the end of a line causes the end-of-line and all subsequent blanks to be ignored. It is a way of splitting a long control-string over several lines.

Some consuming format directives are
~a or ~A Consumes one argument and prints it without escape characters, as princ would ~s or ~S Consumes one argument and prints it with escape characters, as prin1 would ~{str~} Consumes one argument, which must be a list, and prints the members of the list according to the control string str as if they were arguments to an embedded call of format. If str doesn’t consume all the members of the list, it is used again on the next group, and so on. ~#[str1~:;str2~] Uses the control string str1 if there is no argument left or the control string str2 if there is any argument left. If this directive is embedded in the ~ {str ~ } directive, the notion of “argument” is replaced by “member” of the list.

(fresh-line [stream]) ;;function
Outputs a newline to stream, which defaults to standard output and returns T, but only if output is not already positioned at the beginning of a new line. In that case, it does nothing and returns NIL.
(pprint object [stream]) ;;function
Prints a newline and then prints the object using escape characters as appropriate and in a “pretty” indented format. Returns no value. Printing is done into stream, which defaults to the standard output.
(prin1 object [stream]) ;;function
Prints the object using escape characters as appropriate. Returns object. Printing is done into stream, which defaults to the standard output.
(princ object [stream]) ;;function
Prints the object without using escape characters. Returns object. Printing is done into stream, which defaults to the standard output.
(print object [stream]) ;;function
Prints the object using escape characters as appropriate, preceded by a newline and followed by a space. Returns object. Printing is done into stream, which defaults to the standard output.
(read [stream]) ;;function
Reads one S-expression from the input stream, which defaults to standard input, and returns the object represented by that S-expression.
(terpri [stream]) ;;function
Outputs a newline to stream, which defaults to standard output and returns NIL.

6. Arrays - syntax, definition, example

In common lisp arrays have two major sections:

6.1Constructors


(make-array n) ;;function
Creates and returns an array of n elements, indexed from 0 to n − 1. Each element can be any Common Lisp object.

6.2Selectors


(aref array i) ;;function
Returns the ith element of array. i must be a nonnegative integer less than the number of elements of array. Indexing is zero-based. That is, the first element of array is retrieved with (aref array 0). Forms a generalized variable recognizable by setf.

7. Compilers


8. Projects and Software in Common Lisp


Common Lisp is used in many successful commercial applications, the most famous (no doubt due to Paul Graham's promotion) was the Yahoo! Store web-commerce site, which later was rewritten in C++ and Perl.Other notable examples include:

As well, Common Lisp is used by many government and non-profit institutions. Examples of its use in NASA include:

9. External Links




10.Tutorials