Laboratory: Vectors (Labs, CSC 151 2015S)
Summary: In this laboratory, you will explore various aspects of the Vector data type that Scheme provides as an alternative to lists. Suppose we are considering two names for the same collection (list or vector) and want to try to change the first element. Consider the following code. (That is, read it, don’t enter it.) f. What do you expect the output of the following commands to be? (That is, think about the answer, don’t just type it in.) Write a procedure, (vector-sum numbers), which takes one argument, a vector of numbers, and returns the sum of the elements of that vector. You can use the recursion pattern(s) and number-vector-largest as a starting point. If you do, be sure to cite your sources appropriately. It is possible to represent a collection of colors (a “palette”) as a vector. Why would we do so? Well, once we’ve chosen a palette, we can represent an image as a collection of indices into that palette. Such a representation is typically much more compact than representing each color with the full RGB triplet. Write a procedure, (palette-darker! palette), that, given a vector of integer-encoded RGB colors, makes each color in the palette slightly darker (i.e., using irgb-darker). Note that you will not build a new vector. Rather, you will replace each color in the existing vector by the darker version. You may use the recursion pattern(s) from the reading and number-vector-divide! as a starting point. If you do, be sure to cite your sources appropriately. You can test your procedure by converting the vector back to a list and then converting the RGB colors back to strings Write your own version of vector-fill!, call it my-vector-fill!. Remember that vector-fill! takes two parameters, a vector and a value, and puts that value in every position of the vector. Note: You may find that you want to do two things for a particular position: fill the value at that position and recur. Remember that when you want to sequence multiple actions if a test succeeds, you should use a cond or when rather than an if. Write a procedure, (vector-rotate! vec) that rotates the elements in vec. That is, rotate! puts the initial element of vec at the end, the element at position 1 in position 0, the element at position 2 in position 1, and so on and so forth. a. Write a procedure, (vector-reverse vec), that creates a new vector whose elements appear in the reverse order of the elements in vec. b. Write a procedure, (vector-reverse! vec), that reverses vec “in place”. That is, instead of producing a new vector, it rearranges the elements within vec. Write a procedure, (vector-rotate! vec amt), that rotates the values in vec by amt positions. That is, the first amt values in vec move to the end, the value in position amt moves to position 0, the value in position amt+1 moves to position 1, and so on and so forth. Write a procedure, (palette-darkest palette), that takes one argument, a vector of colors, and returns the darkest color in that vector. You can assume that every position in the vector contains a color. In a number of previous exercises, you wrote procedures that iterated over the vector, changing values as you went. (For example, vector-fill! and palette-complement! both had this form.) Summarize the form of a procedure that recurs over vectors, setting the value at each position to a new value, much like the recursion patterns given in the reading. Copyright © 2007-2015 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.) This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. Source.