Lisp Logo from Wikipedia

Common Lisp Cheat Sheet

By Stache, 20161218_

Use of symbolic expressions. Three types of objects : atoms (truc, 1234, *hello*, etc.), lists (sequences of atoms within parens separated by spaces) and strings (aka not the underwear). Stuff’s cas insensitive. Variables are not typed, but objects are.

; Comments are made with semi-colons. Example :
(+ (* (/ 9 5) 60) 32) ; == (60 * 9 / 5) + 62)

Basics

(f x) ; == f(x)
(* 1 [0]), (+ 1 [0]), (- 1 [0]), (/ 1 [0]), (sqrt 1), (expt 1 2), (min 1 [0]), (max 1 [0]), (abs 1), (mod 1), (round 1), (sin 1), (cos 1), (tan 1)
t ; True, Nil ; False, '(stuff) ; Raw input

Lists

'(A B C [0])
(first [L]), (second [L]), ..., (tenth [L]), (nth N [L]), (rest [L]) ; == [L].tail, (last [L]), (length [L]), (car [L]) ; == (first [L]), (cdr [L]) ; == (rest [L])
(cons A [L]) ; == A :: [L], (append [L] [L]) ; == [L] ++ [L], (list [0]) ; create list

Predicates

Type-checking. Return T if same type, NIL if not. Format: (stuffp 1) == 1 typeoff stuff.
(listp 1), (numberp 1), (integerp 1), (stringp 1), (evenp 1), (oddp 1)
(atom 1) ; == not a string nor a list, (= 1 [0]), (> 1 [0]), (< 1 [0]), (<= 1 [0]), (>= 1 [0]), (/= 1 [0]), (null [L]) ; == isEmpty, (equal [L] [L]), (eql 1 2) ; == (1 == 2), (not 1), (and 1 [0]), (or 1 [0])

Var and flow

(setq foo bar) ; == (var foo = bar), (setf foo bar) ; == setq, '(stuff)
(defun name (args) body) ; == def name(args) {body}
(if cond then [else]) ; == if(cond) then else
(cond (test1 result1) [(testn resultn)]) ; == When there is a T, return the corresponding result and stops.
(progn (stuff1) [(stuffn)]) ; Do every stuffs and returns the value of the last stuff.
(loop ...) ; Do NOT use.

Misc

(load name) ; Load and evaluate the file name.
(print stuff) ; Prints stuff and returns the value of stuff.

Example: Factorial

(defun fact (n)
    (if (not (integerp n))
        "Invalid argument"
        (if (< n 2)
            1
            (* n (fact (- n 1)))
        )
    )
)