# Class 36: Vectors

Back to Characters and Strings. On to Pairs and Pair Structures.

This outline is also available in PDF.

Held: Wednesday, 4 November 2009

Summary: We consider vectors, an alternative to lists for storing collections of data.

Related Pages:

Notes:

• Assignment 7 is now ready.
• Fun event Saturday a.m.: Grinnell Destination ImagiNation Fun Run/Walk. More info outside my office or at the back of the room.
• Reading for Friday: Pairs and Pair Structures.

Overview:

• Problems with lists.
• A solution: Vectors.
• Important vector procedures.

## List Deficiencies

• Now that we've worked with lists for a while, we've identified some things that make lists inappropriate for some situations.
• List are expensive to use; to find the length of a list or to access an element late in the list, you need to cdr down the list.
• Lists are fixed; you can't easily change an element of a list.
• At the same time, there are some advantages to lists:
• Lists are dynamic; it is easy to grow and shrink a list.
• Lists are inherently recursive; the type is defined recursively.
• Lists are simple; you can build almost every list operation through just a few basic operations (`car`, `cdr`, `null`, and `null?`).

## An Alternative: Vectors

• Vectors provide an alternative to lists.
• They have two primary advantages:
• Vectors are indexed: You can quickly access elements by number.
• Vectors are mutable: You can change the elements of a vector.
• In order to obtain these benefits, vectors lack some key features of lists. In particular,
• Vectors are static: Once you've created a vector, you cannot change its length.
• Some key vector procedures:
• `(vector val1 ... valn)`: Create a vector
• `(make-vector length val)`: Make a vector of specified length, with duplicates of val as the contents.
• `(vector-ref vector position)`: Extract a value from a vector.
• `(vector-set! vector position newvalue)`: Change an element of a vector.
• `(vector-length vector)`

## Lab

Back to Characters and Strings. On to Pairs and Pair Structures.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Dec 11 09:38:48 2009.
The source to the document was last modified on Fri Aug 21 17:03:06 2009.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2009F/Outlines/outline.36.html`.

You may wish to validate this document's HTML ; ;

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.) This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.