# Vectors

## Exercises

### Exercise 0: Preparation

b. Start DrScheme and tell it not to print the lengths of vectors.

### Exercise 1: Create a Simple Vector

a. In DrScheme's interaction window, type in a vector literal that denotes a vector containing just the two elements 3.14159 and 2.71828. How does DrScheme 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: Specifying Vector Length

a. Tell DrScheme to print the length of vectors.

b. Enter each of the following vector expressions in DrScheme; consider the result (perhaps by examining individual elements with `vector-ref`); and indicate what vector has been created. the result

• #4(0)
• #4(1)
• #4(1 2)
• #2(1 2 3 4)
• (make-vector 4 0)

c. Tell DrScheme not to print the lengths of vectors and reenter each expression. Do your results differ? What do the differences suggest?

### Exercise 3: Summing Vectors

Write a `vector-sum` procedure that takes one argument, a vector of numbers, and returns the sum of the elements of that vector. (You can use `our-vector->list` as a pattern for `vector-sum` -- only a few judicious changes are needed.)

### Exercise 4: Length of Each Element

Write a Scheme procedure `length-of-every-element` that takes as argument a vector of strings and returns a vector containing the lengths of those strings.

```> (length-of-every-element '#("red" "white" "and" "blue"))
#(3 5 3 4)
```

### Exercise 5: Comparing Algorithms

Consider the three versions of the `double-every-element` procedure from reading on vectors.

Which version of `double-every-element` do you prefer? Why?

### Exercise 6: Map for Vectors

Let's generalize the `length-of-every-element` procedure you've just written and the `double-every-element` procedure from reading on vectors.

Write a `vector-map` procedure that takes two arguments, a procedure and a vector, and creates a new vector that contains the result of applying the procedure to each element of the vector, in turn.

### Exercise 7: Extending Map for Vectors

As you may recall, the built-in `map` procedure accepts two or more parameters. If there are additional parameters, it requires that the remaining parameters by lists and creates element i of the result list by applying the procedure to element i of each source list (where i is a number between 0 and length-1, inclusive).

Extend your `vector-map` so that it accepts an arbitrary number of vectors as parameters.

### History

Wednesday, 20 September 2000

• Started.

Wednesday, 8 November 2000

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.