1 / 17

Lists

Lists. "One can even conjecture that Lisp owes its survival specifically to the fact that its programs are lists, which everyone, including me, has regarded as a disadvantage.“ - John McCarthy, "Early History of Lisp". List Representation (1). " ( " followed by zero or more terms and a " ) "

caroun
Download Presentation

Lists

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. Lists "One can even conjecture that Lisp owes its survival specifically to the fact that its programs are lists, which everyone, including me, has regarded as a disadvantage.“ - John McCarthy, "Early History of Lisp"

  2. List Representation (1) • "("followed by zero or more terms and a ")" • Examples: (3 4 5), (word (a-symbol 5) 6 "String"), (), (((4))), ((()) () (()) t ()) • May be arbitrarily long and contain arbitrary types, even other lists • May contain multiple instances of the same element • Elements are in strict order

  3. List Representation (2) • To LISP, a list is either nil or a cons-cell • A cons-cell is an item stored next to a not nil item (usually a pointer) • Classic linked list structure • Example: (A 255 CATS 4.53242)

  4. Constructing Lists with Cons • S-expression stands for Symbolic Expression • Basically, a LISP term • cons creates a dotted-pair of two evaluated S-expressions (cons S-expression S-expression) • Examples • (cons 3 4)→ (3 . 4) • (cons 3 (cons 4 (cons 5 nil))) → (3 4 5) • This is equivalent to (3 . (4 . (5 . nil)))

  5. Constructing Lists with Quote • cons is good for adding one element to the front of a list, but is tedious for long lists • quote provides some syntactic sugar 'S-expression >'(this is a list) (THIS IS A LIST) • quote does not evaluate its argument >'(cons 5 (* 6 7)) (CONS 5 (* 6 7))

  6. Constructing Lists with List • list is used to construct a list of elements that are the results of evaluated expressions • Takes arbitrary number of arguments (list S-expression*) • Examples: • (list 5 (+ 6 7))→ (5 13) • (list '* (+ 6 7) 'x) → (* 13 x) • (list '/ '(+ 6 7) ()) → (/ (+ 6 7) NIL)

  7. Constructing Lists with Append • If you have multiple lists, append can combine them into one list (append S-expression*) • Examples: • (append '(*) '(4 5)) → (* 4 5) • (append '(5 6) '(nil) '(3 (5 6))) → (5 6 NIL 3 (5 6)) • (append '(5 6) nil) → (5 6)

  8. Lists as Trees (1) • The sequence of links can be drawn as a tree

  9. Lists as Trees (2) • Because lists can contain lists, they actually are trees

  10. Deconstructing Lists • Machines on which LISP was originally implemented had two useful machine instructions: • car: contents of address register, the left cell • cdr: contents of decrement register, the right cell • For a linear list, car is like getData() and cdr is like getNext() • Because lists are actually trees, car is like getLeftChild() and cdr is like getRightChild()

  11. >(car '(3 . 5)) 3 >(cdr '(3 . 5)) 5 >(car '(3 4 5 6 7 8)) 3 >(cdr '(3 4 5 6 7 8)) (4 5 6 7 8) >(car (cdr '(2 3 4 5))) 3 Letters a and d can be embedded between c and r to compose multiple car/cdr calls (car (car '((2 . 7) . 3)) is (caar '((2 . 7) . 3)) (cdr (cdr '(4 5 6))) is (cddr '(4 5 6)) (car (cdr '(2 3 4 5))) is (cadr '(2 3 4 5)) Up to four levels of nesting supported Car and Cdr Examples

  12. Other Accessors (first S-expression) (rest S-expression) • first and rest are the same as car and cdr • second, third, fourth, … , tenth also defined (nth integer S-expression) • nth retrieves element indicated by integer • Zero based • Examples: >(nth 0 '(4 5 6 7)) 4 >(nth 2 '(r e (s f) y)) (S F)

  13. Association Lists • Used to represent mappings • Lists of dotted-pairs Example: ((+ . "add") (- . "subtract")) • assoc returns the pair associated with a given key, nil if not found >(assoc '- '((+ . "add") (- . "subtract"))) (- . "subtract")

  14. Types of Equality >(assoc '(4 5) '(((2 4) . A) ((4 5) . B))) NIL • Default equality test is eql • eq does a pointer comparison • eql same as eq, but can also compare numbers of the same type • = is for numbers only, but can compare numbers of different types, e.g. (= 3 3.0)→ T • equal is the most general, and compares structure (slowest) • Can get around this using keyword arguments >(assoc '(4 5) '(((2 4) . A) ((4 5) . B)) :test 'equal) ((4 5) . B) • We’ll learn more about these later

  15. Updating Association Lists • assoc returns the first pair matching the input • (assoc 'c '((c . 5) (c . 6))) → (C . 5) • To change an association, put a new pair in front of it >(cons (cons 'name "Jacob") '((name . "Eric"))) ((NAME . "Jacob") (NAME . "Eric")) • acons is a shortcut for this (acons key value association-list) >(acons 'name "Jacob" '((name . "Eric"))) ((NAME . "Jacob") (NAME . "Eric"))

  16. Predicates for Lists • consp returns T if its argument is a cons • atom returns T if the argument is not a cons • listp returns T if its argument is a list, meaning a cons or nil • null returns T if its argument is nil • endp is like null, but requires a list

More Related