[plt-scheme] Question about Vectors

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Feb 12 14:30:16 EST 2010

This is a possibly badly worded exercise. I don't want you to change  
the length of the vector. Instead I want a function with this contract:

    ;; [Vector Number] -> Void

It should change v so that at each position it contains the average of  
its two neighbors and itself. Since this doesn't work for the leftmost  
and rightmost position, you need to think about what to do there. For  
you: set them to '-; it'll help you find the mistakes in the code.

-- Matthias



On Feb 12, 2010, at 12:58 PM, Gadfly wrote:

> 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
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.