Other useful functions Obs: a simple list always ends with nil; - caar, cadr, cdar, cddr, caaar, ... (caadr x) <=> (car (car (cdr x))) caddr: the third element in the list - list-length: > (list-length '(a (b c) d)) 3 - nth n xs: returns the n-th element in list xs (0-indexed) - first, second, third, ..., tenth, rest (third x) <=> (nth 2 x) > (first '(1 2 3)) 1 > (rest '(1 2 3)) (2 3) - last: returns the last cons cell of the list > (last '(a b c d)) (d) > (last '(a b c . d)) (c . d) > (last '(a b c d) 3) (b c d) Predicates - listp: returns true if its argument is a list > (listp '(a b c)) T -endp: returns true if the given list is null (usually used to check if the end of a list has been reached) > (endp nil) T > (endp '(a b c)) NIL - null > (null nil) T - atom: returns true if the argument is an atom > (atom 'abc) T > (atom '(a b)) nil - equal: returns true if the two arguments are structurally similar > (equal '(a (b c)) '(a (b c))) T > (equal '(a (b c)) '(a b c)) nil -eq: returns true if the two arguments are identical > (setq x '(a b c) y (cdr x)) (b c) > (equal y '(b c)) T > (eq y '(b c)) NIL > (eq y (cdr x)) T - member: looks for an element in a list and returns the corresponding cons cell > (member 'b '(a b c)) (b c) > (member 'a '(f (a g) b c a d a e)) (a d a e) (member '(c d) '(a b (c d) e)) NIL > (member '(c d) '(a b (c d) e) :test #'equal) ((c d) e) See also: member-if, member-if-not Logical operators - and: if all arguments evaluate to true, it returns the last of them; if any of the arguments evaluates to false, none of the following ones are evaluated > (and t (+ 5 4)) 9 - or > (or t (+ 5 4)) T Conditional expressions - if: > (if (listp 5) (+ 5 4) (- 5 3)) 2 Obs: Anything except nil is evaluated as true > (if 5 1 2) 1 - cond (cond (test-1 expr-1) (test-2 expr-2) ... ) evaluates the tests until it finds one which is not nil, which is evaluated and returned > (setq x 0) 0 > (setq valx (cond ((< x 0) '(x is less than zero)) ((= x 0) '(x is equal to zero)) (t '(x is greater than zero)))) (x is equal to zero) Recursive functions Example: length of a list. (defun len (ls) (if (null ls) 0 (+ (len (cdr ls)) 1)))