# Class 31: Vectors

Back to Early Break. On to Input and Output.

Held: Monday, 27 October 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 mutable.

Related Pages:

Assignments

Notes:

• I hope you had a great vacation.
• Expect exam 2 tomorrow.

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

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

## Detour: Sequencing Statements with `begin`

• 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!` or `display`, that have side effects.
• Otherwise, all you're doing is computing values and then forgetting about them.

## Lab

• Do the lab.
• Note that I'll probably ask you to reflect in the next class session.

## History

Thursday, 28 August 2003 [Samuel A. Rebelsky]

• Created automatically.

Back to Early Break. On to Input and Output.

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 Dec 9 13:59:49 2003.
The source to the document was last modified on Mon Sep 1 13:30:51 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Outlines/outline.31.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu