Fundamentals of Computer Science I (CSC-151.02 2000F)


Vectors

Exercises

Exercise 0: Preparation

a. If you have not done so already (or if you've forgotten what it says), please reread the reading on vectors.

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

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.

Notes

History

Wednesday, 20 September 2000

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.

This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/vectors.html

Source text last modified Wed Nov 8 10:16:29 2000.

This page generated on Wed Nov 8 10:21:13 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu