Show Menu
Cheatography

Common Lisp Cheat Sheet (DRAFT) by

A guide to commonly used function and macros in Common Lisp.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Common Lisp Sequences

Creating Collec­tions
(vector elements ...)
Generic vector of args.
(make-­array size [opts])
Returns empty n-dime­nsional array.
(list elements ...)
Creates a singly­-linked list of args.
Operating on Sequences
General
(length sequence)
Returns length of sequence.
(elt sequence index)
Returns the corres­ponding element of sequence.
Vectors
(vecto­r-pop array)
Destru­ctively removes the last element of array.
(vecto­r-push new-el array)
Destru­ctively appends new-el to array.
 
(If the array fill-p­ointer indicates full, nothing occurs)
(vecto­r-p­ush­-extend new-el array [opts])
Destru­ctively appends new-el to array.
 
(will extend an array with a fill-p­ointer if necessary)
Search and Sort
(count item sequence [opts])
Returns number of times item appears.
(position item sequence [opts])
Returns index of item or nil.
(sort sequence predicate [opts])
Sorts sequence by predicate function. (i.e. #'<)
i.e. (sort '(1 4 3 2) #'<)
=> '(1 2 3 4)
Filters
(count-if predicate sequence)
Returns number of elements that satisfy predicate
(remove-if predicate sequence)
Removes all elements that satisfy predicate. (non-d­est­ruc­tive)
 
These functions have 'if-not' variants.
Iteration
(map result­-type func sequences ...)
Returns the result of applying an n-arg function to the current element of n sequences.
(mapcar function sequences)
Same as above, but for lists.
(map-into result­-se­quence func sequences ...)
Places results into 'resul­t-s­equ­ence' rather than creating a new sequence.
(reduce func sequence [opts])
Applies func to elements in twos to return a single value.
i.e. (reduce #'+ '(1 2 3))
=> (+ 1 2) => (+ (+ 1 2) 3) => 6
(loop ... [opts])
   

Assignment and Binding

(setf place val &rest args)
Sets value of 'place' to val. Works in pairs.

i.e. (setf 'a 1
'b 2)
Sets 'a to 1 and 'b to 2.


(let ((var-a value)
(var-b value))
Creates a context where var-a and var-b are bound to their respective values.

(let* ((var-a value)
(var-b value))
Same as above, but bound sequen­tially.
i.e. Bindings can refer to previous bindings.

(defpa­rameter var-name value) Creates a global variable.