Vectors

Exercise 1

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

Exercise 2

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?

Exercise 3

Develop 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

Develop a Scheme procedure join-strings that takes two arguments, both vectors of strings, equal in length, and returns a third vector of the same length, containing the results of concatenating corresponding elements of the argument vectors.

> (join-strings (vector "sect" "ram" "wee") (vector "ion" "pant" "knights"))
#("section" "rampant" "weeknights")

Exercise 5

Develop a Scheme procedure reverse-vector that takes any vector as argument and returns a vector of the same length, containing the same elements, but in reverse order.

Exercise 6

The dot product of two equally long vectors of numbers is the sum of the products of corresponding elements of those vectors. Develop a Scheme procedure dot-product that computes the dot product of two given vectors of numbers.

> (dot-product (vector 2 3 4) (vector 5 6 7))
56  ; (2 x 5) + (3 x 6) + (4 x 7) = 56
> (dot-product (vector 3 1 4 1 6) (vector 2 7 1 8 3))
43
> (dot-product '#() '#())
0

This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~stone/courses/scheme/labs/vectors.xhtml

Validated as XHTML 1.1 by the World Wide Web Consortium Cascading Style Sheet validated by the World Wide Web Consortium

created November 7, 1997
last revised October 4, 2001

John David Stone (stone@cs.grinnell.edu) and Ben Gum (gum@cs.grinnell.edu)