; Vectors ; - another datatype that can store multiple objects in order ; - have a lot of similarities to lists, but also some important differences ; - vectors look like like lists but have # in front ; - also you cannot change the size of a vector, so you cannot cons or cdr ; a vector ; - vectors also have an important advantage, which we we may not see... ; because of the fixed way that they are stored, vector-ref is a lot quicker ; than list-ref ; - the way list-ref is implemented by DrScheme, is it walks through the list ; and stops when it have found the item it wants ; - in contrast, vector-ref can jump right to the item it wants.... ; before we get started, it is a good idea to do (print-vector-length #f) ; keeps Dr. Scheme from using shorthand to represent some vectors ; vector commands.... what are three ways of creating the vector #(1 1 1) (make-vector 3 1) ; what happens if I type (make-vector 3), it returns #(0 0 0) (vector 1 1 1) '#(1 1 1) ; you may not be able to modify this in other versions of scheme ; my-list->vector, changes the input list into a vector (define my-list->vector (lambda (ls) (apply vector ls))) ; it is a bit trickier to change a vector into a list, here is the version from the ; reading (define our-vector->list (lambda (vec) ; remaining is the number of items left to copy (let kernel ((remaining (vector-length vec)) (result null)) (if (zero? remaining) result (let ((position (- remaining 1))) (kernel position (cons (vector-ref vec position) result))))))) ;; now let's write vector-string-append, which takes in a vector of strings ;; and returns the result of string-appending all the strings together into ;; one long string ;(define vector-string-append ; (lambda (vec) ; (apply string-append (vector->list vec)))) ; let's write it without vector->list (define vector-string-append (lambda (vec) ; remaining is the number of items left to copy (let kernel ((remaining (vector-length vec)) (result "")) (if (zero? remaining) result (let ((position (- remaining 1))) (kernel position (string-append (vector-ref vec position) result))))))) ; so now (vector-string-append (vector "abc" "def" "ghi")) ; returns "abcdefghi"