Warm-up! What is your favorite color? CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB Brown Orange Yellow Green ((repeated bf 3) '(cat dog hat bat)) Predict the Output bat bat (bat) ((repeated bf 3) '(cat dog hat bat)) (cons () (list () (cons () ()))) bat bat (bat) butfirst of a sentence ALWAYS returns a sentence Never quote the output of scheme! (repeated bf 3) returns a procedure that takes a word or a sentence. ( () () (()) ) ( () () () () ) ( () () (() ()) ) 1
(cons () (list () (cons () ()))) (empty? ()) ( () () (()) ) ( () () () () ) ( () () (() ()) ) Review what cons, append and list do (empty? ()) (empty? ) empty? returns true for empty and words Don t use it with! (empty? ) (null? ) empty? returns true for empty and words Don t use it with! 2
(null? ) (null? (cons ())) null? returns true for null Don t use it with words and! (null? (cons ())) (assoc x ((a 1) (b 2) (x 3) (x 4)) An empty word counts as something inside the list. So it is not null? 3 (x 3) (x 4) (assoc x ((a 1) (b 2) (x 3) (x 4)) (member x (a b c d x e f g)) 3 (x 3) assoc just returns the first match. Returns the entire pair not just the car of the cdr (x 4) 3
(member x (a b c d x e f g)) (member? x (a b c d x e f g)) Returns: (x e f g) member returns the rest of the sentence starting with the thing you were looking for. It is a semi-predicate member? returns just #t or #f (member? x (a b c d x e f g)) (member? x (a b c d (x) e f g)) member returns the rest of the sentence starting with the thing you were looking for. It is a semi-predicate member? returns just #t or #f (member? x (a b c d (x) e f g)) (cons 1 2) member doesn t look inside of sub (1 2) (1. 2) 4
(cons 1 2) The second argument to cons should be a list otherwise you end up with this weird dot. (map cons 1 (2)) (1 2) (1. 2) Remember cons pulls out the underwear of the second argument and drops the first argument in. (1 2) (1. 2) ((1 2)) (map cons 1 (2)) (map cons (1) (2)) (1 2) (1. 2) ((1 2)) Map takes in a procedure and one or more LISTS! There should be the same number of as there are arguments that the procedure takes in! (1 2) (1. 2) ((1 2)) Fill in the blank (map cons (1) (2)) ((1 2) (A B)) (1 2) (1. 2) Map takes the car of each list and calls the procedure cons with them. i.e. (cons 1 2) it does that for each element in the and puts the result in a list! (map cons (1 A) ((2) (B))) (map cons ((1) (A)) ((2) (B))) (map cons (1 A) (2 B)) ((1. 2)) 5
Fill in the blank ((1 2) (A B)) (= x x) (map cons (1 A) ((2) (B))) (list (cons 1 (2)) (cons A (B))) (map cons ((1) (A)) ((2) (B))) (list (cons (1) (2)) (cons (A) (B))) (map cons (1 A) (2 B)) (list (cons 1 2) (cons A B)) (= x x) = only works with numbers! For words/ use equal? (= 1 1) (= 1 1) Numbers are words! (every first awesome) The quote is implicit on numbers a awesome (a w e s o m e) 6
(every first awesome) a awesome Every returns a sentence even if it is given a word! 0 1 (a w e s o m e) (depth 1234567) 0 1 0 1 (depth 1234567) (depth ((a)(b))) 1 0 1 2 3 4 (depth ((a)(b))) Which line has an error? 7
1 2 3 Correct the error! 4 Which line has an error? List or Sentence? New line 4: (+ 1 (reduce max (map depth lst))) item item list-ref for item for /words 8
assoc assoc Assoc are LISTS first first car for first for /words reduce reduce reduce for accumulate for /words 9
map map map for (and it is way cooler than every!) every for /words filter filter filter for keep for /words 10