;;;;;;;;;; a. (define (dot-product v1 v2) (do [(i 0 (add1 i)) (sum 0 (+ sum (* (vector-ref v1 i) (vector-ref v2 i))))] [(= i (vector-length v1)) sum])) b. (define (get-matrix-row mat i) (vector-ref mat i)) c. (define (get-matrix-col mat j) (do [(i 0 (add1 i)) (out-vect (make-vector (vector-length mat)))] [(= i (vector-length mat)) out-vect] (vector-set! out-vect i (vector-ref (vector-ref mat i) j)))) d. (define (matrix-mult m1 m2) (do [(i 0 (add1 i)) (out-mat (make-vector (vector-length m1)))] [(= i (vector-length m1)) out-mat] (vector-set! out-mat i (do [(j 0 (add1 j)) (mat-row (make-vector (vector-length (vector-ref m1 i))))] [(= j (vector-length (vector-ref m1 i))) mat-row] (vector-set! mat-row j (dot-product (get-matrix-row m1 i) (get-matrix-col m2 j))))))) e. (define (sum-dot-products m1 m2) (do [(i 0 (add1 i)) (sum 0 (+ sum (dot-product (get-matrix-row m1 i) (get-matrix-col m2 i))))] [(= i (vector-length m1)) sum])) ;;;;;;;;;; a. (1 3 5 7 9 8 6 4 2) () (1) (3 5 7 9 8 6 4 2) () (1) (2) (3) (5 7 9 8 6 4) () (1) (2) (3) (4) (5) (7 9 8 6) () (1) (2) (3) (4) (5) (6) (7) (9 8) () (1) (2) (3) (4) (5) (6) (7) (8) (9) () (1 2 3 4 5 6 7 8 9) b. (1 3 5 7 9 8 6 4 2) (1 3 5 7 9) (8 6 4 2) (1 3 5) (7 9) (8 6) (4 2) (1 3) (5) (7) (9) (8) (6) (4) (2) (1) (3) (5) (7) (9) (8) (6) (4) (2) (1 3) (5 7) (8 9) (4 6) (2) (1 3 5 7) (4 6 8 9) (2) (1 3 4 5 6 7 8 9) (2) (1 2 3 4 5 6 7 8 9) ;;;;;;;;;; a. (define (insert-into-bucket n vect) (vector-set! vect (modulo n 10) (append (vector-ref vect (modulo n 10)) (list n)))) b. (define (list->bucket lst v) (cond [(empty? lst) (void)] [else (begin (insert-into-bucket (first lst) v) (list->bucket (rest lst) v))])) ;;;;;;;;;; (define (vector-reverse v) (do [(i 0 (add1 i)) (j (sub1 (vector-length v)) (sub1 j)) (out-vect (make-vector (vector-length v)))] [(= i (vector-length v)) out-vect] (vector-set! out-vect j (vector-ref v i)))) ;;;;;;;;;; a. Advantages: o It only takes a single function call to get any element from a vector (vector-ref ...) o Easy to do iteration on vectors Disadvantages: o Vectors have a fixed-size (cannot add elements to vectors) o Tough to recurse on vectors b. "filter" is a function that takes in two parameters: 1. A function that takes in some scheme item and returns a boolean. We will call this the "testing" function. 2. A list of scheme items (each having the same data type as the scheme item in parameter 1) The function returns a list of scheme items corresponding to the elements in the list that pass the testing function. In other words, "filter" just filters out all elements in a list that do not pass our testing function. The function call returns: (list (list 3 2) (list 6 8)) Why? --The lambda function tests to see if the sum of the first two elements of a list is greater than 0. For the list given, this is true for the first nested list, false for the second nested list, and true for the third nested list. Thus, the return value is the original list, excluding the second nested list.