ANSI%20Common%20Lisp - PowerPoint PPT Presentation

About This Presentation
Title:

ANSI%20Common%20Lisp

Description:

Conses (cons, car and cdr) Cons. Combine two objects into a two-part pair. ... Takes a sequence and a comparison function of two arguments, and returns a ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 27
Provided by: Whit83
Learn more at: https://www.cs.uml.edu
Category:
Tags: 20common | 20lisp | ansi

less

Transcript and Presenter's Notes

Title: ANSI%20Common%20Lisp


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.gt(setf x (cons a b))(A . B)gt(car
    x)Agt(cdr x)B
  • Box notation

4
Conses (lists)
  • Lists can be represented as conses.gt(cons a
    nil)(A)gt(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).gt (setf x
    (list a (list b c) d)(A (B C) D)

5
List Functions
  • listgt (list a b c)(A B C)
  • car and cdrgt (car (a b c))Agt (cdr (a b
    c))(B C)
  • listp (returns T if the argument is either a cons
    or a NIL)gt(listp (a b c))T

6
List Functions
  • consp (returns T for any cons)gt(consp (a b
    c))T
  • atom (returns T for any none-cons)gt(atom NIL)T
  • append (concatenates any number of
    lists)gt(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.)gt(eq x x)Tgt(eq (cons 1
    2) (cons 1 2))NIL
  • eql (returns T iff object1 and object2 are eq or
    the same character/number.)gt(eql x x)Tgt(eql
    (cons 1 2) (cons 1 2))NIL

8
List Functions -- Equality
  • equal (returns T if its arguments prints
    same.)gt(equal (cons 1 2) (cons 1 2))Tgt (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.gt(setf x (a b c) y
    (copy-list x))(A B C)

10
List Functions
  • Difference between copy-list and setfgt(setf x
    (a b c))(A B C)gt(setf y x)(A B C)gt(eql x
    y)Tgt(setf (car y) 'aa)gtx(AA B C)

11
List Functions -- Access
  • first, second, third, fourth, , tenthgt(third
    (a b c d))C
  • cxxxxr (cadr, caddr, cadddr)gt(caddr '(a b c d e
    f))C
  • nthgt(nth 0 (a b c))A
  • nthcdrgt(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.gt(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.gt(maplist (lambda (x) x) (a b c))((A B
    C) (B C) (C))

13
List Functions
  • lengthgt(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.gt(subseq (a b
    c d) 1 3)(B C)gt(subseq (a b c d) 1)(B C D)

14
List Functions
  • reversegt(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.gt(sort (4 3 7 5 1)
    gt)(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.gt(every oddp
    (1 3 5))T(some evenp (1 2 3))Tgt(every lt
    (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.gt(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
  • Substitutiongt(setf x '(and (integerp x) (zerop
    (mod x 2))))(AND (INTEGERP X) (ZEROP (MOD X
    2)))gt(substitute 'y 'x x)(AND (INTEGERP X)
    (ZEROP (MOD X 2)))gt(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.
  • adjoingt(adjoin b (a b c))(A B C)gt(adjoin d
    (a b c))(D A B C)
  • uniongt(union (a b c) (c b s))(A C B S)

20
Sets
  • intersectiongt(intersection (a b c) (b b c))(B
    C)
  • set-differencegt(set-difference (a b c d e) (b
    e))(A D C)
  • The union, intersection and set-difference
    functions dont 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 its looking for. By default, it
    compares objects using eql. The test keyword
    lets you change the comparison function.gt(member
    (a) ((b) (a) (z)))NILgt(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.gt(member a ((a b) (c d)) key
    car)((A B) (C D))If theres an element whose
    car is a.
  • member-ifFind an element satisfying some
    predicate.gt(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.gt(setf x (a))gt(push b x)(B
    A)gt(pop x)B

24
Stacks
  • pushnewTakes the same arguments as push(x, y).
    If object x already exists in list y, then x
    wont be pushed in anymore.gt(setf x
    (a))gt(push b x)(B A)gt(pushnew b x)(B A)

25
Dotted Lists
  • Proper listsEither a NIL or a cons whose cdr is
    a proper list.
  • Dotted Listsgt(setf pair (cons a b))(A . B)
  • Lists can be represented as dotted lists.gt(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. gt(setf trans (( .
    add) (- . subtract)))(( . add) (- .
    subtract))gt(assoc trans)( . add)
  • The assoc function can take key and test
    keywords.
Write a Comment
User Comments (0)
About PowerShow.com