CS 3723
 Programming Languages 
   Lisp Lists  


Lists in Lisp: Using car, cdr, and cons.

  • Stopping evaluation: Sometimes we don't want to evaluate a list or an atom. quote is another Lisp special function that doesn't evaluate its argument, but instead returns it unevaluated. A single quote mark, without the parens, gives a shorthand version of the longer form.

    Quote or ', Preventing Evaluation
    > (quote (a b c))
    (A B C)
    > '(a b c)
    (A B C)
    > (a b c)
    Error: The function A is undefined.
    

  • car and cdr tear lists apart: car returns the first element of a list, and cdr returns the remainder of the list with the first element deleted.

    car and cdr: Tearing Lists Apart
    > (car '(a b c))      ;;; first element is a
    A
    > (cdr '(a b c))      ;;; rest of list is (b c)
    (B C)
    > (car (cdr '(a b)))  ;;; cdr gives (b), and car of that is b
    B
    > (car (a b c))       ;;; lisp tries to evaluate (a b c)
    Error: The function A is undefined.
    

  • cons puts two lists together: cons assembles a list from its car and cdr.

    Cons: Assembling Lists
    > (cons 'a '(b c))
    (A B C)
    > (cons (car '(a b c)) (cdr '(a b c)))
    (A B C)
    


Lists in Lisp: Using list, append, eval, and apply

  • list and append give two more ways to create new lists:

    List: Creating Lists
    ; list takes any seq of S-expressions
    ; and makes a list out of them:
    > (list 'a 'b 'c)
    (A B C)
    > (list '(a) '(b c) 'd)
    ((A) (B C) D)
    > (list '(a (b c) d) '((e) f ((g))))
    ((A (B C) D) ((E) F ((G))))
    > (list 'a '(b c) nil 'd () )
    (A (B C) NIL D NIL)
    ; Note: list keeps empty lists
    
      
    Append: Tack Lists Together
    ; append takes any sequence of lists and
    ; combines them into a single list:
    > (append '(a b) '(c d))
    (A B C D)
    > (append '(a (b c) d) '((e) f ((g))))
    (A (B C) D (E) F ((G)))
    > (append '(a b) nil '(c) () )
    (A B C)    ; append drops empty lists
    > (append 'a '(b c))
    *** - APPEND: A is not a list
    > (append '(a b) 'c)    
    (A B . C) ; more on this notation later
    

  • Evaluating lists -- Another Look Look back at what was said under an earlier header, "Evaluating lists." Let's see how Lisp can evaluate using actual functions apply and eval:

    Eval and Apply
    > (+ 3 4 5)
    12
    > (eval (+ 3 4 5))
    12
    > (apply '+ '(3 4 5))
    12
    > (apply (car '(+ 3 4 5)) (cdr '(+ 3 4 5)))
    12
    


List Library Functions: Common Lisp has a large number of library functions. Here are a few of them:

  • List Functions:

    List Functions
    first: same as car 
    second  third  fourth  ... last 
    rest: same as cdr
    append  list (illustrated above)
    Suppose > (setq c '( (a b) c d)) caar: "car of the car", (caar x) is (car (car x)) is A cadr: "car of the cdr", (cadr x) is (car (cdr x)) is C cdar: "cdr of the car", (cdar x) is (cdr (car x)) is (B) cddr: "cdr of the cdr", (cddr x) is (cdr (cdr x)) is (D)

  • Other Predicates: (Boolean-valued functions, they return t or nil)

    Predicates
    atom: is its arg an atom?
    null: is its arg nil or ()?
    numberp: is its arg a number?
    listp: is its arg a list?
    member: two args, does the 1st appear in the 2nd (a list)?
    equal: do its 2 args look alike? (= is only for nums)
    

( Revision date: 2014-04-01. Please use ISO 8601, the International Standard Date and Time Notation.)