CS 334: Lisp FAQ

This list should include all Lisp forms needed for the assignments.

Atoms

t                    ;; true
nil                  ;; false
A, Cow, moo          ;; symbols

Quoted Forms

'abc             ;; Unevaluated symbol
'(1 2 3)         ;; The list (1 2 3), which is not evaluated.  So:
                       (+ 1 2)  =>  3
                      '(+ 1 2)  =>  (+ 1 2)

Boolean expressions

(eq x y)        ;; Returns t if x and y are the same number or 
                ;; the same list.  Note: there are other equality
                ;; tests (eql, equals, etc.).  For 334, the distinctions
                ;; should be not important and eq should sufficient 
                ;; for the problems we do.

                        (eq 1 1)  =>  t
                        (eq 1 2)  =>  nil
                      (eq 'A 'A)  =>  t
                      (eq 'A 'B)  =>  nil
                    (eq nil nil)  =>  t

(< x y)         ;; Arithmetic comparison. 
                ;; (Other simple ops include: >, >=, or, and, not)

                        (< 1 2)  =>  t
                        (< 2 1)  =>  nil

(atom x)        ;; Return t if x is an atom and nil if x is a list

                                (atom 'A)  =>  t
                               (atom nil)  =>  t
                    (atom (car '(A nil)))  =>  t
                    (atom (cdr '(A nil)))  =>  nil

(member x l)    ;; Return nil if x is not in the list,
                ;; or t (or some other non-nil value) if x is
                ;; in the list.

                        (member 'A '(A B))  =>  t
                        (member 'C '(A B))  =>  nil

Arithmetic expressions

(+ e1 ... en)   ;; Sum the numbers e1 ... en.  (*,-,/ are all similar)

                    (+ 1 2 3)  =>  6

Other operators include mod, sin, cose, etc.

                    (mod 7 3)  =>  1

Lists

nil             ;; The empty list

(cons x ls)     ;; The list containing x, followed by the elements in ls

                             (cons 'A nil)  =>  (A)
                             (cons 'A (B))  =>  (A B)
                            (cons nil nil)  =>  (nil)

(car ls)        ;; The first element of the list ls

                               (car '(A B C))  =>  A
                        (car (cons A '(B C)))  =>  A

(cdr ls)        ;; The list ls, excluding the first element

                                    (cdr nil)  =>  nil
                               (cdr '(A B C))  =>  (B C)
                        (cdr (cons A '(B C)))  =>  (B C)

(append l1 l2)  ;; The list containing the elements from l1, 
                ;; followed by the elements from l2

                    (append '(A B) '(C D))  =>  (A B C D)

(length ls)     ;; The length of the list

                                 (length nil)  =>  nil
                            (length '(A B C))  =>  3

Conditionals

(cond (p1 e1) ... (pn en))  ;; Evaluates each pi in order until it finds
                            ;; a true guard.  It then evaluates the
                            ;; expression for that guard:

                                (cond ((eq 0 1) 2) (t 3))  =>  3
                                (cond ((eq 0 0) 2) (t 3))  =>  2


(if p e1 e2)                ;; More readable syntax for a cond defining 
                            ;; the usual "if-then-else".  Use cond if you 
                            ;; have more than two conditions to test.

                                         (if (eq 0 1) 2 3)  =>  3
                                         (if (eq 0 0) 2 3)  =>  2

Function definitions

(defun f ls body)          ;; Defines a function named f, with arguments ls

                            (defun fact (x) 
                                    (cond ((eq x 0) 1)
                                            (t (fact (- x 1)))))

                            (defun mult(x y) (* x y))


(lambda ls body)           ;; Anonymous function expression

                            ((lambda (x) (* x x)) 3)  =>  9

Higher-order Operations

(mapcar #'f l)    ;; Map the function f over the elements of l, creating
                  ;; a new list.

                            (mapcar #'atom '(A (2 3) B))  =>  (t nil t)
                    (mapcar #'(lambda (x) (+ x 2)) '(1 3))  =>  (3 5)

                    ;; The #' in front of a lambda or function name
                    ;; extracts the "functional object" assocated
                    ;; with that function or lambda expression.  The
                    ;; functional object for a function "f" records
                    ;; the number of arguments expected by f, the
                    ;; code for the body of f, etc.


(apply #'f (e1 ... en))   ;; Equivalent to (f e1 ... en)

                             (apply #'* '(2 2 3))  =>  12
                        (apply #'fact '(1 2 3 4))  =>  (1 2 6 24)

(funcall #'f e1 ... en)   ;; Equivalent to (f e1 ... en)

                                 (funcall #'* 2 2 3)  =>  12
                            (funcall #'fact 1 2 3 4)  =>  (1 2 6 24)

For all of these higher-order operations, note that you do not need #' in front of a parameter name that stores a function you have passed into a function. You only need #' when you refer directly to the symbol for a function definition:

        (defun reverse-map (f l) 
                (mapcar f (reverse l)))

        (reverse-map #'- '(1 2 3))       =>   (-3 -2 -1)

Impure Features

Here for completeness only. You should not use these in your programs unless explicetly told to do so.

(defvar ls '(a b c))       ;; declares variable ls with given value
(rplaca ls x)              ;; updates ls to be '(x b c)
(rplacd ls (x))            ;; updates ls to be '(a x)