CSC153 2004S, Class 15: Vectors Admin: * IF YOU MISS CLASS, PLEASE DROP ME A NOTE EXPLAINING WHY (preferably in advance) * Why is Friday the 13th unlucky? * 13 is unlucky because certain religions are concerned about the 13th apostle (Judas) * Friday is unlucky because those same religions note a number of events happened on Friday (It's on the Internet, so it must be true.) * Theorem: It is no less common than any other date. * To maximize unluckiness. * Is "Internet" generally capitalized? * "Internet" should be capitalized, because it's a proper noun. * Is "anal retentive" hyphenated? * It depends on how you use it. * "Sam is anal retentive." is not hyphenated * "Sam is an anal-retentive editor." is hyphenated. * Questions on the exam? * Sam unavailable this weekend. * Ask questions on Monday. * Questions on yesterday's class? * Cool new reading: Higher-order procedures Overview: * Strengths and weaknesses of lists * An alternative: Vectors * Key vector procedures * Lab Do you already know all of this from your work with arrays and linked lists in whatever languages you know? Good things about lists (in Scheme): * Defined recursively: Either null or cons of a value and a list. * Dynamic: Easy to extend (as much as you want) (or shrink). * Heterogeneous: Can contain multiple types of data Lists have some limitations: * Lists are not mutable (or at least Sam hasn't taught you how to mutate them) * Accessing elements in lists is slow. ("Random" access is not random.) * Heterogeneous: Can contain multiple types of data Vectors are the solution! * Mutable * Fast to access elements * Not defined recursively * Not dynamic How do we recurse over vectors? * With a list, we can cdr * With vectors, we can use an accompanying counter that we iterate recursively Try the lab! Observation: * vector-set! does not return the vector * You can write your own variant that does * The name of a mutator should always end with ! * On the exam, > (value->string (list 1 2 3)) "(1 2 3)" * When you need to do more than one thing in a branch of an if , use begin (if (test) (begin (vector-set! vec ....) (recurse vec (+ pos 1))) ...)