CSC151.02 2003F, Class 31: Vectors Admin: * I hope you enjoyed your break. * Congratulations Nate! * Read "Output in Scheme". * Read "Input in Scheme". * Expect an exam tomorrow. * No, there's no lab writeup due tomorrow. * Missing: Reese and Michael. Overview: * Problems with lists * A solution: Vectors * Common vector operations * Detour: The begin construct * Lab * Reflection (maybe) What is good about lists? * Heterogeneous: You can store lots of kinds of values in one list. * Collect values (probably good) * Possible to extract individual values within the list and then do things with them * Easy to use * Wide range of built-in procedures to manipulate them * Recursively defined -> makes it more naturual to write recursive procedures What is less good about lists? * Slow to find the ith element or the length of a list * You can't change the individual elements of a list An alternative: The vector * Like a pair, but with as many cells as you want * Fast to get an individual element * Length known all the time, so fast to get the length * Heterogeneous: You can store lots of kinds of values in one list. * Collect values (probably good) * Possible to quckly extract individual values within the vector and then do things with them * Wide range of built-in procedures to manipulate them * NOT recursively defined * However, they cannot grow and shrink easily (lists can) When writing programs that need collections of values, you need to decide whether it is better to be able to shrink and grow the collection (lists) or to access arbitrary elements quickly (vectors) Some of the details: * Creating a vector * (vector 'a 'b 'c) - a lot like list * (make-vector 5 'a) - make a vector of a specified length containing all the same values * #('a 'b 'c) - write explicitly * Extracting information about a vector * (vector-length vec) * (vector-ref vec pos) - gives you the value at the specified position * Changing information in a vector * (vector-set! vec pos newvalue) - sets the value at a particular position * Checking if a value is a vector * vector? Why are they called vectors? * Because computer scientists are really bad at naming things. * Because they are like points in n-dimensional vector spaces. Exercise: Let's write a program that builds a vector of size n and fills it with the numbers 1 to n * Notes: * Need to build the vector * Need to write a kernel that steps through the vector recursively (define bobmarley (lambda (n) (let ((bunnywailer (make-vector n 'trouble))) (letrec ((petertosh (lambda (pos) (if (= pos 0) (vector-set! bunnywailer 0 1) (vector-set! bunnywailer pos (+ pos 1)))))) (petertosh (- n 1)) ) ) ) ) (define iota-vector (lambda (n) (let ((vec (make-vector n 'trouble))) (letrec ((kernel (lambda (pos) (if (< pos 0) vec (begin (vector-set! vec pos (+ pos 1)) (kernel (- pos 1))))))) (kernel (- n 1)) ) vec ) ) ) Side note: The begin construct lets you group a number of operations you want to do together. It is primarily useful in conditionals. Try the lab!