1 / 26

ANSI Common Lisp

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

bill
Download Presentation

ANSI Common Lisp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ANSI Common Lisp 3. Lists 20 June 2003

  2. Lists • Conses • List Functions • Trees • Sets • Stacks • Dotted Lists • Assoc-lists

  3. 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 notation

  4. 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)

  5. 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

  6. 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)

  7. 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

  8. 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.)

  9. 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)

  10. 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)

  11. 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)

  12. 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))

  13. List Functions • length>(length ‘(a b c d))4 • subseqThe 2nd argument (required) is the position of the 1st element to beincluded. 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)

  14. 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)

  15. 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

  16. 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.

  17. 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)))))

  18. 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)))

  19. 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)

  20. 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.

  21. 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))

  22. Sets -- member • member function The :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)

  23. 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

  24. 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)

  25. 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)

  26. 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.

More Related