CS 227 lecture -*- Outline -*- Bring: a loaf of bread (in a plastic sleeve). show how car, cdr and cons work on the loaf. Time: Cut out the math if not enough time... * Lists (section 1.4) ** what is a list? like a loaf of bread, the wrapper is the empty list, taking off a slice is car, what's left is the cdr... like a sentence (a list of words), the period at the end is like () a list is a sequence of objects can be empty *** notation for values and examples of values: ------------ LIST VALUES () (1 2 3) (1 2 1) (1 1) (1) (who dat) ( () () ) def: Let T be a type of value. A value of type (list T) is either () or (x1 ... xn) where x1, ..., xn are values of type T. ---------- say how to read (list T) - make a list of people (2 TAs) (line up facing the audience) - cons another TA to the front of the list ----------------- PICTURES OF LISTS ____ () ] ____] ___________ (1 2 3) 1 | 2 | 3 ] ___________] ___________ (1 2 1) 1 | 2 | 1 ] ___________] ___________ (who dat) who | dat ] ___________] __________ (() ()) * | * ] _|_____|__] v v ___ ___ ] ] ___] ___] ------------------ *** not sets (cheese cheese) is not the same as the list (cheese), think of this as a shopping list *** not bags more like a grocery bag, but even that isn't quite right, (g o d) is not the same as (d o g) order matters ** making lists Do this at the computer first, then summarize below '() (cons 1 '()) (cons 1 (cons 2 '())) ; note which end is evaluated first (cons 'sym '()) etc. use with define other lists, defined and nested expressions *** cons doesn't change anything. (define foo (cons 1 '())) foo (define bar (cons 2 foo)) bar foo what kind of argument is the second argument to cons? *** cons doesn't care what is in the list already (lists are not sets) (cons 'cheese (cons 'cheese '())) *** nested lists -------------------------------- NESTED LISTS ((1)) ______ * ] (cons ___|__] (cons 1 '()) v '()) ___ 1 ] ___] (2 (1)) ________ (cons 2 2 | * ] (cons _____|__] (cons 1 '()) v '())) ___ 1 ] ___] ((2) (1)) ________ (cons (cons 2 '()) * | * ] (cons _|___|__] (cons 1 '()) v v '())) ___ ___ 2 ] 1 ] ___] ___] ----------------------------- These are a natural progression. Tell students the value of indentation. *** practice exercise: make (3), ((33)), (() ((3))) more if time: (() ((3)) (() ((3)))) Stress that in solving such things, work from the inside *** summary **** the empty list -------------- LIST OBJECTS '() : (list T) -------------- read this as "for all types T, '() has type list of T **** cons forms a new list from an item and a list: -------------- cons :(-> (T (list T)) (list T)) -------------- read this as "for all types T..." -------------- def: let T be a type of object. An object of type (list T) is either '() or (cons x l) where x has type T and l has type (list T). -------------- note the difference between the type (list T) and the expression (list 'T) *** quote - try just typing in (3 2) -- what happens? - what is this like we have seen already? - can use quote or ' to prevent evaluation. e.g., '() gives empty list. '(1 2 a) '(ten) - quote is only good for literal lists what is (define ten 10) (define eleven 11) '(ten eleven)? what if we want the values? (ten eleven) doesn't work? - but (cons ten '()) or (cons ten (cons eleven '())) also '(+ 1 2) ==> (+ 1 2) but (+ 1 2) ==> 3 can think of '(1 2) as easier way to type (cons '(cons 2 '())) cons is the real thing ---------------- (cons 'x '(y z)) ==> (x y z) ---------------- ---------------- QUOTATION FOR LISTS '(x y z) ==> (x y z) '(x y z) = (cons 'x (cons 'y (cons 'z '()))) ---------------- * Data Modeling numbers, symbols and lists can represent most anything discuss how to model shopping lists (cheese cheese milk) ((2 cheese) (1 milk)) map from a game (adjacency list) ((the-pit (south the-dungeon) (north hall-3) (east hall-45)) (the-dungeon (north the-pit)) ...) * Taking lists apart (section 1.5) - car and cdr are selectors, they take lists apart names historical ** examples - do some samples of car - do some samples of cdr use quoted lists and defined stuff - composition (car (cdr .... )) ** practice ------------ (define famous-programmers '((Ada Lovelace) (Alan Turing))) ------------ get Turing, Lovelace do some with hidden lists (in sections) ** summary --------------- (car '(x y z)) ==> x (cdr '(x y z)) ==> (y z) car: (-> ((list T)) T) cdr: (-> ((list T)) (list T)) both require argument to be nonempty. --------------- test for that using the procedure null? (later) - do list of people demo ------------ LIST EQUATIONS for all x:T, l:(list T), (car (cons x l)) = x (cdr (cons x l)) = l for all l:(list T), l is non-empty implies (cons (car l) (cdr l)) = l ------------- ** cadr etc. common to combine car and cdr explain and show cadr etc. up to 4 works (guaranteed) ** dotted pairs (important mostly as an example of what not to do) (give example) set of non-empty lists is a subset of pairs ---------- (DOTTED) PAIRS def: let S and T be types. A value of type (pair S T) is (x . y) where x:S, y:T. An object of type (pair S T) is (cons x y) where x:S, y:T. cons: (-> (S T) (pair S T)) car : (-> ((pair S T)) S) cdr : (-> ((pair S T)) T) Theorem: for all types T, (list T) contains (pair T (list T)). ---------- Thus, one can use cons in 2 ways: to make pairs, 2nd arg anything to make lists, 2nd arg must be a list