CSC151 2007S, Class 32: Vectors Admin: * Due: Project Report 1 * Code + Text Files + Report * Assigned: Exam 2 * Thanks for your patience on Wednesday. Eldest son came in first, so we're heading off to state in a month or so. (On a Saturday, this time.) * Have a great break! Overview: * Problems with lists. * A solution: Vectors. * Lab. /What is a List?/ * A potentially empty collection of values * Primary operations: * car - the first element in the list * cdr - the rest of the list * cons - add something to the front * null - build an empty list * null? - check if a list is empty * Secondary operations: * append * list-ref gives you "any elment you want" * Suppose we have the list, letters = ("a" "b" "c" "d" "e") * How do you get element 3? (list-ref letters 3) (car (cdr (cdr (cdr letters)))) * Whichever you use still implicitly does three cdrs * Suppose you want to set element 2 to "k" * You'll need to rebuild the list (define revised-letters (cons (car letters) (cons (car (cdr letters)) (cons "k" (cdr (cdr (cdr lst))))))) These are the two big flaws of lists: * Slow to access elements later in the list * Lists are not (naturally) mutable The solution: Vectors * Mutable * Equal access time for each element For 1c: You'll need both make-vector and vector-set! (define vec (make-vector 2)) (vector-set! vec 0 ...) (vector-set! vec 1 ...) vec NOTE: vector-set! does not return a value. Hence, you only use it with vectors that you have already named!