# Class 15: Vectors

Back to Pairs and Pair Structures. On to Higher-Order Procedures (1).

Held: Friday, 13 February 2004

Summary: Today we consider vectors, which serve as alternates to lists. Vectors differ from lists in three primary ways: vectors have fixed size, vectors are indexed, and vectors are naturally mutable.

Related Pages:

Assignments:

Notes:

• There will be time for questions on the exam.
• I will be unavailable this weekend.

Overview:

• Problems with lists.
• A solution: Vectors.
• Side note: The `begin` construct.
• Lab.

## 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?`).

## Vectors: Solving the Problems of Lists

• For applications in which you need some of the features that lists fail to provide, Scheme provides the alternative of vectors.
• Vectors provide constant-time access to their elements.
• Vectors are naturally mutable. That is, one of the key operations on vectors is set an element of the vector.
• Scheme also provides procedures to mutate lists, but those are used only in special cases and their careless use can lead to very confusing results.
• In order to provide these benefits, Scheme restricts vectors to be of a fixed size.
• As you solve different problems, you'll need to decide which features are most important to you (the dynamic size of lists or the mutability and constant-time access of vectors).

## Detour: Sequencing Statements with `begin`

• Operations whose primary purpose is to change a value (rather than to create a new value) are called side-effecting operations.
• We sometimes find that we need to do more than one thing when working with side-effecting operations.
• For example, we might modify a vector and then recurse, as in `list->vector` from the reading.
• Scheme provides the simple `begin` construct for do more than one thing in sequence.
• Here's the form
```(begin
expression-1
expression-2
...
expression-n)
```
• Note that this form is only useful when you have operations, like `vector-set!`, that have side effects.
• Otherwise, all you're doing is computing values and then forgetting about them.

## History

Back to Pairs and Pair Structures. On to Higher-Order Procedures (1).

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 May 7 09:43:11 2004.
The source to the document was last modified on Tue Jan 13 10:26:10 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Outlines/outline.15.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu