Laboratory: Vectors
Summary:
In this laboratory, you will explore various aspects of the Vector
data type that Scheme provides as an alternative to lists.
Preparation
Open the reference on vectors in a new tab or window.
Exercises
Exercise 1: Create a Simple Vector
a. In the Interaction pane, type in a vector literal that denotes a
vector containing just the two elements 3.14159 and 2.71828. How does
MediaScript display the value of this vector?
b. Create a vector that contains the same two values by using the
vector procedure.
c. Create a vector that contains the same two values by using the
make-vector and vector-set! procedures.
Exercise 2: Modifying Lists and Vectors
Consider the following code. (That is, read it, don't
enter it.)
> (define aardvark (list 1 2 3 4))
> (define baboon aardvark)
> (define aardvark (cons 5 (cdr aardvark)))
> (define chinchilla (vector 1 2 3 4))
> (define dingo chinchilla)
> (vector-set! chinchilla 0 5)
a. What do you expect the output of the following commands to be?
> (list-ref aardvark 0)
_____
> (list-ref baboon 0)
_____
b. Verify your answer experimentally. (That is, you can type in the
commands now.)
c. What do your results suggest about Scheme?
d. What do you expect the output of the following commands to be?
(That is, think about the answer; don't just type it in.)
> (vector-ref chinchilla 0)
_____
> (vector-ref dingo 0)
_____
e. Verify your answer experimentally. (You can type the commands now.)
f. What do your results suggest about vectors and lists in Scheme?
Exercise 3: From Lists to Vectors
In the reading on vectors, we saw that it was possible
to implement list->vector and
vector->list by using more primitive
operations (particularly vector-set! and
vector-length).
Here's the definition of list->vector
and its kernel.
The design of list->vector is a bit
subtle. Explain what the kernel does. If you're not completely
sure, you may want to add a call to display
inside the kernel, as in
(display (list 'l2v-kernel! lst pos vec)) (newline)
Exercise 4: Darkening Vector-Based Palettes
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.
For example, here is a palette that
represents the colors in the rainbow.
(define rainbow-palette
(list->vector
(map color->rgb
(list "red" "orange" "yellow" "green" "blue" "indigo" "violet"))))
Write a procedure, (palette-darker!
palette), that, given a vector of RGB
colors, makes each color in the palette slightly darker. (Note that
you will not build a new vector. Rather, you will
replace each color in the existing vector by
the darker version. That means you should not be using
vector->list or
list->vector in your implementation.)
You can test your procedure by converting the vector back to a list
and then converting the RGB colors back to strings
> (palette-darker! rainbow-palette)
> (map rgb->string (vector->list rainbow-palette))
Exercise 5: Building Palettes
Here is a procedure that builds a list-based palette from an image by
randomly selecting n values from the image.
Using this procedure as a guide to creating colors, and
list->vector as a guide to building
vectors, write (palette-fill!
palette image),
which fills palette with colors randomly selected
from the image. (Why don't we have an n as
a parameter? Because you should use the palette size for the number
of colors to fill in.)