# Class 16: Vectors

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

Held: Friday, 14 February 2003

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:

• Are there questions on exam 1? I've added a Q&A section to the exam.
• I spent the last two nights doing other class prep. Since I'm behind in grading, I've extended the deadline for the exam.
• The first reading is somewhat long. Sorry.
• Alumna Brenda Johnson is giving a talk on Fermat's little theorem today at 4:30 in Science 2424. As an odd coincidence, I saw a note on Fermat's little theorem this morning as I was looking at material for this course. One point of extra credit for attending.

Overview:

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

## 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 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

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Thursday, 13 February 2003 [Samuel A. Rebelsky]

• Filled in the details.

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

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 Tue May 6 09:20:31 2003.
The source to the document was last modified on Thu Feb 13 23:13:58 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.16.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu