26
ANSI Common Lisp 3. Lists 20 June 2003

ANSI Common Lisp

  • Upload
    bill

  • View
    46

  • Download
    1

Embed Size (px)

DESCRIPTION

ANSI Common Lisp. 3. Lists 20 June 2003. Lists. Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists. Conses (cons, car and cdr). - PowerPoint PPT Presentation

Citation preview

Page 1: ANSI Common Lisp

ANSI Common Lisp

3. Lists

20 June 2003

Page 2: ANSI Common Lisp

Lists

• Conses

• List Functions

• Trees

• Sets

• Stacks

• Dotted Lists

• Assoc-lists

Page 3: ANSI Common Lisp

Conses (cons, car and cdr)

• ConsCombine two objects into a two-part pair. The first part is car and the second is cdr.>(setf x (cons ‘a ‘b))(A . B)>(car x)A>(cdr x)B

• Box notationa b

Page 4: ANSI Common Lisp

Conses (lists)

• Lists can be represented as conses.>(cons ‘a nil)(A)>(cons 'a (cons 'b (cons 'c nil)))(A B C)

• A list can have any kind of object as elements, including another list (nested list).> (setf x (list ‘a (list ‘b ‘c) ‘d)(A (B C) D)

nil

a b c

nil

a

nil

a

c

nil

b

d

Page 5: ANSI Common Lisp

List Functions

• list> (list ‘a ‘b ‘c)(A B C)

• car and cdr> (car ‘(a b c))A> (cdr ‘(a b c))(B C)

• listp (returns T if the argument is either a cons or a NIL)>(listp ‘(a b c))T

Page 6: ANSI Common Lisp

List Functions

• consp (returns T for any cons)>(consp ‘(a b c))T

• atom (returns T for any none-cons)>(atom NIL)T

• append (concatenates any number of lists)>(append '(a (b c)) '(d e))(A (B C) D E)

Page 7: ANSI Common Lisp

List Functions -- Equality

• eq (returns T iff object1 and object2 are in the same memory location.)>(eq x x)T>(eq (cons 1 2) (cons 1 2))NIL

• eql (returns T iff object1 and object2 are eq or the same character/number.)>(eql x x)T>(eql (cons 1 2) (cons 1 2))NIL

Page 8: ANSI Common Lisp

List Functions -- Equality

• equal (returns T if its arguments prints same.)>(equal (cons 1 2) (cons 1 2))T> (equal '(a b c) '(a (b) c))NIL

• equalp (returns T iff object1 and object2 are equal, char-equal, or=, or are cons whose cars and cdrs are equalp; or are hash tables with same test function and number of entries whose keys are all associated with equalp values.)

Page 9: ANSI Common Lisp

List Functions

• copy-listThe new list has the same elements as the original, but not eql to.>(setf x ‘(a b c) y (copy-list x))(A B C)

nil

a b c

x

nily

Page 10: ANSI Common Lisp

List Functions

• Difference between copy-list and setf>(setf x ‘(a b c))(A B C)>(setf y x)(A B C)>(eql x y)T>(setf (car y) 'aa)>x(AA B C)

nil

a b c

x

y

Page 11: ANSI Common Lisp

List Functions -- Access

• first, second, third, fourth, …, tenth>(third ‘(a b c d))C

• cxxxxr (cadr, caddr, cadddr…)>(caddr '(a b c d e f))C

• nth>(nth 0 ‘(a b c))A

• nthcdr>(nthcdr 2 ‘(a b c))(C)

Page 12: ANSI Common Lisp

List Functions – mapping functions

• mapcarTakes a function and one or more lists, and returns the result of applying the function to elements taken from each list, until some list runs out.>(mapcar #’list ‘(1 2 3 4) ‘(a b c))((1 A) (2 B) (3 C))

• maplistTakes the same arguments as mapcar, but applies the function on successive cdrs of the lists.>(maplist #’(lambda (x) x) ‘(a b c))((A B C) (B C) (C))

Page 13: ANSI Common Lisp

List Functions

• length>(length ‘(a b c d))4

• subseqThe 2nd argument (required) is the position of the 1st element to be included. The 3rd argument (optional) is the position of the first element not to be included.>(subseq ‘(a b c d) 1 3)(B C)>(subseq ‘(a b c d) 1)(B C D)

Page 14: ANSI Common Lisp

List Functions

• reverse>(reverse ‘(a (b c) d))(D (B C) A)

• sortTakes a sequence and a comparison function of two arguments, and returns a sequence with the same elements sorted according to function.The sort function is destructive. The sequence given to sort may be modified.>(sort ‘(4 3 7 5 1) #’>)(7 5 4 3 1)

Page 15: ANSI Common Lisp

List Functions

• every and someTake a predicate and one or more sequences. If given only one sequence, they test whether the elements satisfy the predicate. If given more then one sequences, they test whether the elements, drawn one at a time from all sequences, satisfy the predicate.>(every #’oddp ‘(1 3 5))T(some #’evenp ‘(1 2 3))T>(every #’< ‘(1 2 3) ‘(4 5 6))T

Page 16: ANSI Common Lisp

Trees

• The conses (lists) can be considered as binary trees, where the car represents the left sub-tree and the cdr represents the right sub-tree.

nil

a

dbnil

c

(A (B C) D)

Page 17: ANSI Common Lisp

Trees

• General Operation Forms on TreesRecursing down both the car and cdr.

>(copy-tree ‘(a (b c) d))(A (B C) D)

(defun copy-tree (tr) (if (atom tr) tr (cons (copy-tree (car tr)) (copy-tree (cdr tr)))))

Page 18: ANSI Common Lisp

Trees

• Substitution>(setf x '(and (integerp x) (zerop (mod x 2))))(AND (INTEGERP X) (ZEROP (MOD X 2)))

>(substitute 'y 'x x)(AND (INTEGERP X) (ZEROP (MOD X 2)))

>(subst 'y 'x x)(AND (INTEGERP Y) (ZEROP (MOD Y 2)))

Page 19: ANSI Common Lisp

Sets

• Lists can be considered as sets. Each element of a list is a member of the set it represents.

• adjoin>(adjoin ‘b ‘(a b c))(A B C)>(adjoin ‘d ‘(a b c))(D A B C)

• union>(union ‘(a b c) ‘(c b s))(A C B S)

Page 20: ANSI Common Lisp

Sets

• intersection>(intersection ‘(a b c) ‘(b b c))(B C)

• set-difference>(set-difference ‘(a b c d e) ‘(b e))(A D C)

• The union, intersection and set-difference functions don’t guarantee the original order of elements will be preserved.

Page 21: ANSI Common Lisp

Sets -- member

• member function

Tests whether an object is a member of a set. If yes, it returns the part of list beginning with the object it’s looking for. By default, it compares objects using eql. The :test keyword lets you change the comparison function.>(member ‘(a) ‘((b) (a) (z)))NIL>(member ‘(a) ‘((b) (a) (z)) :test #’equal)((A) (Z))

Page 22: ANSI Common Lisp

Sets -- member

• member functionThe :key keyword is used to specify a function to be applied to each element of the list before comparison.>(member ‘a ‘((a b) (c d)) :key #’car)((A B) (C D))If there’s an element whose car is a.

• member-ifFind an element satisfying some predicate.>(member-if #’oddp ‘(2 3 4))(3 4)

Page 23: ANSI Common Lisp

Stacks

• Lists can be considered as stacks (FILO).

• push and pop(push x y) pushes x onto the front of list y; pop(y) removes and returns the 1st element of list y.>(setf x ‘(a))>(push ‘b x)(B A)>(pop x)B

Page 24: ANSI Common Lisp

Stacks

• pushnewTakes the same arguments as push(x, y). If object x already exists in list y, then x won’t be pushed in anymore.

>(setf x ‘(a))>(push ‘b x)(B A)>(pushnew ‘b x)(B A)

Page 25: ANSI Common Lisp

Dotted Lists

• Proper listsEither a NIL or a cons whose cdr is a proper list.

• Dotted Lists>(setf pair (cons ‘a ‘b))(A . B)

• Lists can be represented as dotted lists.>’(a . (b . (c . nil)))(A B C)

a b

Page 26: ANSI Common Lisp

Assoc-lists

• Assoc-list is a list of conses. It can be used as mapping or hash table. >(setf trans ‘((+ . “add”) (- . “subtract”)))((+ . “add”) (- . “subtract”))>(assoc ‘+ trans)(+ . “add”)

• The assoc function can take :key and :test keywords.