Skip to content

Latest commit

 

History

History
125 lines (106 loc) · 10.4 KB

lisp-cheatsheet.org

File metadata and controls

125 lines (106 loc) · 10.4 KB

Common Lisp Cheatsheet

This cheatsheet covers most of the stuff needed for COMP-348. Every function referenced in the lectures and tutorials should appear here. If there’s something that I missed, please let me know!

Functions and variables

FunctionExampleOutputDescription
defvar(defvar x 10)X = 10Creates a new global variable
defun(defun f (x) ...)F(X) = …Creates a new function
set(set 'x 20)X = 20Sets the value of a quoted symbol.
setq(setq x 30)X = 30Same as set except you don’t need to quote
setf(setf x 40)X = 40Same as setq except you can set inside lists
let(let ((x 1)) x)X = 1Creates a locally-scoped variable inside the block
let*(let ((x 1) (y (+ x 2))) y)3Same as let, but you can use previous bindings
defconstant(defconstant x 10)X = 10Creates a new global constant. Cannot change value.
boundp(boundp 'x)tReturns t if symbol is bound to a value

Functions

FunctionExampleOutputDescription
defun(defun f (x) ...)F(X) = …Creates a new function
lambda(lambda (x) (* x 2))<function>An instance of a lambda function. Can be assigned to variables.
funcall(funcall #'oddp 1)TCalls the given function with it’s arguments
apply(apply #'oddp '(1))TCalls the given function with a list of it’s arguments

Lists

FunctionExampleOutputDescription
null(null (list 1 2 3))NILReturns t if the list is empty or value is nil.
listp(listp (list 1 2 3))TReturns t if the element passed in is a list
list(list 1 2 3)‘(1 2 3)Returns a new list of the elements passed to it.
cons(cons 1 '(2 3))‘(1 2 3)Constructs a list given a head and a tail.
car(car '(1 2 3))1Returns the head of a list.
cdr(cdr '(1 2 3))‘(2 3)Returns the tail of a list.
append(append '(1) '(2) '(3))‘(1 2 3)Appends multiple lists together.
length(length '(1 2 3))3Returns the length of the given list.
member(member 2 '(1 2 3))‘(2 3)Returns the list starting at the first occurence of the element
copy-list(copy-list '(1 2 3))‘(1 2 3)Returns a copy of the list, copying each element.
mapcar(mapcar #'oddp '(1 2 3))‘(T NIL T)Returns a new list with a given function applied to every element

Conditionals

FunctionExampleOutputDescription
if(if t 10 20)10If the condition is true, run the first block, otherwise the second.
and(and 10 20)20Return the last value, or nil if they’re not all true.
or(or nil 10)10Return the first non-nil value, or nil if non are true.
cond(cond (nil 1) (t 2))2Runs the first body who’s condition evaluates to true.
case(case 'a ('a 1) (t 3))1Runs the first body who’s condition evaluates to true.
when(when t (print 1) 2)1, 2Runs the body if the condition evaluates to true.
unless(unless nil (print 1) 2)1, 2Runs the body if the condition evaluates to false.
not(not nil)TReturns T if argument is NIL, returns NIL if argumnent in non-nil.

Comparisons

FunctionExampleOutputDescription
=(= 1 2)nilUsed for comparing the equality of numbers only.
<(< 1 2)tChecks if all arguments are smaller than the following arguments
>(> 1 2)nilChecks if all arguments are greater than the following arguments
<=(<= 1 2)tChecks if all arguments are smaller than or equal to the following arguments
=>(=> 1 2)nilChecks if all arguments are greater than or equal to the following arguments
eq(eq 1 2)nilChecks if two numbers or characters have the same value.
eql(eql 1 2)nilChecks if two values refer to the same object or are eq.
equal(equal 1 2)nilChecks if two values are equal. Numbers, symbols, strings, lists, etc.

Numerical functions

FunctionExampleOutputDescription
numberp(numberp 1)TReturns true if it’s argument is a number.
zerop(zerop 0)TReturns true if it’s argument is zero.
evenp(evenp 2)TReturns true if it’s argument is an even number.
oddp(oddp 3)TReturns true if it’s argument is an odd number.
plusp(plusp -1)NILReturns true if it’s argument is a positive number
integerp(integerp 1.2)NILReturns true if it’s argument is an integer
+(+ 1 2 3 4 5)15Takes the sum of all it’s arguments
-(- 5 4 3 2 1)-5Subtracts all of it’s arguments
*(* 1 2 3 4 5)120Takes the product of all it’s arguments
/(/ 20 2 2)5Divides all of it’s arguments
gcd(gcd 64 72)8Finds the greatest common denominator of it’s arguments
abs(abs -3)3Finds the absolute value of it’s argument
sin(sin 0)0Finds sin(x)
cos(cos 0)1Fins cos(x)

Output

FunctionExampleOutputDescription
print(print "Hello")“Hello”Prints it’s arguments to stdout
format(format t "X: ~a" 'Y)“X: Y”Print’s it’s formatted arguments to stdout
terpri(terpri)“\n”Prints a newline. Equivalent to (format t "\~%").

Predicates

FunctionExampleOutputDescription
boundp(boundp 'x)TReturns true if symbol is bound to a value
null(null (list 1 2 3))NILReturns true if the list is empty or value is nil.
atom(atom "hello")TReturns true if it’s argument is an atom or nil
listp(listp '(1 2))TReturns true if it’s argument is a list or nil.
numberp(numberp 1)TReturns true if it’s argument is a number.
evenp(evenp 2)TReturns true if it’s argument is an even number.
oddp(oddp 3)TReturns true if it’s argument is an odd number.
zerop(zerop 0)TReturns true if it’s argument is zero.
plusp(plusp -1)NILReturns true if it’s argument is a positive number
integerp(integerp 1.2)NILReturns true if it’s argument is an integer
stringp(stringp "a")TReturns true if it’s argument is a string.
symbolp(symbolp 'a)TReturns true if it’s argument is a symbol.
keywordp(keywordp :a)TReturns true if it’s argument is a keyword.

Miscellaneous

FunctionExampleOutputDescription
quote(quote a)AReturns it’s arguments literally, as passed in.
''aASame as quote, but a nice shorthand.
progn(progn (print 1) 2)1, 2Evaluates every expression in it’s body and returns the last value.