[plt-scheme] Question about Vectors

From: Gadfly (dyrueta at gmail.com)
Date: Fri Feb 12 12:58:08 EST 2010

Hi All --

Is there a way to reduce the size of a vector strictly through
mutation?

(ex. (vector 1 2 3 4 5)  -> (vector 2 3)

I know it sounds dumb, but I'm asking with regards to HtDP exercise
43.1.6.

The exercise asks the student to design a function which computes 3-
item sliding averages of a list or vector of numbers.  The minimum
length of the list or vector is 3, and the length of the resulting
list or vector will be 2 less than the original size.

;list-3-average: (listof numbers) -> (listof numbers)
;with list of numbers ( >= n 3,) length of resulting list is (n - 2);
otherwise, empty list
;computes the sliding-average of a list of numbers with an interval of
size 3  (ex. (0,2), (1 3), etc)
; (equal? (list-3-average '(1 4 7)) '(4))

The exercise also asks for the function 3 ways:
-- by consuming a list of numbers and producing a new one;
-- by consuming a vector of numbers and producing a new one;
-- by mutating the vector consumed

It's the last part I'm stuck on.  I can see how to replace each
element of a vector in the interval (0, (n -2)) with sliding-averages
through mutation, while leaving the last two elements unchanged --

(equal? (vector-3-average-mut (vector  3 10 11))  (vector 8 10 11))

-- but I can't see how it is possible to get the result defined by the
function contract --

(equal? (vector-3-average-mut (vector  3 10 11))  (vector 8))

-- that is, a truncated vector, strictly through mutation. Am I
missing something totally basic, or is my provisional solution the one
sought by the exercise?

Many thanks!
Dave Yrueta


Posted on the users mailing list.