Lunds Tekniska Högskola
Datalogi och numerisk analys
TAI
Short Common Lisp Reference List
;;;; four semicolons
headline
;;; three semicolons
left
justified comment
;; two semicolons
comment
on a new line indented in accordance with the program text
(.......) ; one semicolon the comment is written to the right of the program text
Function definition
(defun name (parameters) {documentation} definition)
Example
(defun foo(x)
"prints the value of the argument"
(print x)
) ;foo
Arithmetic functions
(+ x1 ... xn) | addition |
(* x1 ... xn) | multiplication |
(- x1 ... xn) | subtraction |
(/ x1 ... xn) | division |
(1+ x) | increment with 1 |
(1- x) | decrement with 1 |
(expt x n) | x raised to power n |
(exp n) | e raised to power n |
(sqrt x) | square root |
(abs x) | absolute value |
(max x1 ... xn) | max value |
(min x1 ... xn) | min value |
(float x) | convert integer (or rational) to float |
(truncate x) | truncate integer |
(round x) | round floating number |
(mod x y) | modulus |
(rem x y) | remainder |
Assignment
(setq symbol value) | symbol is assigned value |
(setf place value) | object at place is assigned value |
Predicates (numbers)
(< x1 ... xn) | less ? |
(<= x1 ... xn) | less or equal ? |
(> x1 ... xn) | greater ? |
(>= x1 ... xn) | greater or equal ? |
(= x1 ... xn) | equal ? |
(/= x1 ... xn) | not equal ? |
Predicates (type tests)
(zerop x) | 0 ? |
(plusp x) | > 0 ? |
(minusp x) | < 0 ? |
(oddp x) | odd ? |
(evenp x) | even ? |
(numberp x) | number ? |
(integerp x) | integer ? |
(floatp x) | floating ? |
(rationalp x) | rational ? |
(complexp x) | complex ? |
(symbolp x) | symbol ? |
(consp x) | cons ? |
(listp x) | list ? |
(atom x) | atom ? |
(null x) | nil ? |
(endp l) | is the list l the empty list (nil)? |
Equal tests
(eq s1 s2) | ;are the symbols s1 and s2 equal? |
(= n1 n2) | ;are the numbers n1 and n2 equal? |
(eql x1 x2) | ;are (the symbols, numbers, characters) x1 and x2 equal? |
(equal obj1 obj2) | ;are the objects obj1 and obj2 equal? |
Output functions
(princ x stream) | ;sends x to stream without special characters |
(prin1 x stream) | ;sends x to stream with special characters |
(terpri stream) | ;sends new-line to stream |
(fresh-line stream) | ;sends new-line to stream if not already at a new line |
(print x stream) | ;sends x and new-line to stream with special characters |
(write x stream options) | ;a printed representation of x is output |
(error string obj-1 ... obj-n) | ;a fatal error is signalled, format is applied and sent to the error handling mechanism |
(format string obj-1 ... obj-n) ~a the object is printed as by princ ~d the object is printed in decimal radix ~e the object is printed as a floating-point number using exponential notation ~f the object is printed as a floating-point number with decimal digits ~s the object is printed as by prin1 ~t tabulator ~% new-line ~& new-line if not at the beginning of a new line ~| new page |
;the printed representation of the objects is output in accordance with the format specified in string |
Input functions
(read stream) | ;reads a Lisp object from stream |
(read-char stream) | ;reads a character from stream |
(read-line stream) | ;reads a line fromstream |
(peek-char peek-type stream) | ;the next character from stream is read, but not removed - if peek-type is T white space is skipped |
Array structures
(make-array size) | ;creates an array with size elements |
(make-array rank) | ;creates a multi-dimensional array, rank is a list indicating the size of the dimensions |
(vector e-1 ... e-n) | ;creates a vector consisting of the elements e-i |
(arrayp obj) | ;test if obj is an array structure |
(aref arr ix-1 ... ix-n) | ;returns the value of the element at ix-1 ... ix-n inarr |
(setf (aref arr ix1 ... ixn) element) | ;stores element at at ix-1 ... ix-n in arr |
(array-rank arr) | ;returns the number of dimensions of arr |
Record structures
(defstruct name {docstring} slot-name-1 ... slot-name-n) | |
(defstruct name {docstring} (slot-name-1 default-1))... (slot-name-n default-n)) | |
(make-name :slot-name-1 expr-1 ... :slot-name-n expr-n) | ;creates a record structure |
(name-slot-name record) | ;returns the value of the slot-name element in record |
(setf (name-slot-name record) new-value) | ;stores new -value in slot-name of record |
(name-p record) | ;is record a name-structure? |
(copy-name record) | ;creates a copy of record |
Property lists
(get symbol property) | ;returns the value associated with property from the property list of symbol |
(remprop symbol property) | ;removes property from the property list of symbol |
(setf (get symbol property) value) | ;associatesvalue to property in the property list of symbol |
(symbol-plist symbol) | ;returns the property list ofsymbol |
(progn uttryck1 ... uttryckn) | ;evaluatesexpr-11 ... expr-n sequentially, returns the value of expr-n |
(prog1 uttryck1 ... uttryckn) | ;evaluatesexpr-11 ... expr-n sequentially, returns the value of expr-1 |
Conditional expressions
(cond (test-1 consequent-1-1 consequent-1-2 ...) (test-2) (test-1 consequent-1-3) ... (test-nconsequent-1-n) ) |
(if test then-expression {else-expression} ) |
(case value (alt-1 expr-1 ... expr-n) ... (alt-n expr-1 ... expr-n) (otherwise expr) ) |
(when test expr-1 ... expr-n ) |
;evaluate expr-1 ... expr-n if test is true |
(unless test expr-1 ... expr-n ) |
;do not evaluate expr-1 ... expr-n if test is true |
Repetition
(loop expr-1 ... expr-n ) |
;infinite repetition - terminated by (return value) |
(dolist (var list-form {result}) {declarations} expr-1 ... expr-n ) |
(dotimes (var count-form {result}) {declarations} expr-1 ... expr-n ) |
(do (var {init-form {repetition-form}}) {test result-form} {declarations} expr-1 ... expr-n ) |
Blocks
(block block-name expr-1 ... expr-n ) |
(return-from block-name value) | ;return from named block |
(return value) | ;return from implicit block |
List Predicates
(member obj l) | ;is the object obj a (top-level) member of list l ? |
List functions
(car l) | ;the first element in the list l |
(cdr l) | ;the list l , except the first element |
(first l) | ;the first element in the list l |
(second l) | ;the second element in the list l |
... | |
(tenth l) | ;the tenth element in the list l |
(nth number l) | ;the number-th element in the list l |
(last l) | ;the last element in the list l |
(rest l) | ;the list l , except the first element |
(cons obj-1 obj-2) | ;the cons whose car is obj-1 and whose cdr is obj-2 |
(list obj-1 ... obj-n) | ;creates a list of the objects |
(append l-1 ... l-n) | ;joins the elements of the lists into a single list |
(make-list size :initial-element x) | ;creates a list with size elements initiated to x |
(copy-list l) | ;creates a copy of l |
(remove s l) | ;creates a copy of l , with all s elements removed |
(reverse l) | ;creates a reversed copy of l |
(subst new old l) | ;creates a copy of l |
(intersection l1 l2) | ;creates a list consisting of all elements that are |
(union l1 l2) | ;creates a list consisting of all elements in either of the lists |
(butlast l) | ;creates a list of the elements in l, except the last element |
(length l) | ;length of list l |
(assoc key alist) | ;returns the first top level element of alist whose car equals key |
Map functions
(mapcar fn list1 ... listn) | ;applies fn to the elements in the list(s), returns the values in a list |
(maplist fn liist-1 ... list-n) | ;applies fn applicerar fn to the tails of the list(s), returns the values in a list |
Let
(let ( (var-1 {init-1}) ... (var-n {init-n}) ) expr-1 ... expr-n ) |
Labels
(labels ( (fn-1 argument-1 definition-1) ... (fn-n argument-n definition-n) ) expr-1 ... expr-n ) |