# Class 28: Vectors

Back to Association Lists. On to Deep Recursion.

Held: Friday, 7 March 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:

Due

Notes:

• I understand that many of you found homework three quite long. I apologize for the length. Asking questions earlier (and in class) would probably help.
• As I mentioned in advance, I will not be here today. Mr. Stone will be teaching class. Reese will be taking notes for posting to the EBoard at a later date.
• No written homework next week! No reading this weekend, either. Good luck recovering from the homework.

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

## 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 `set!`, 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 on Monday.

## History

Thursday, 16 January 2003 [Samuel A. Rebelsky]

• First version, created mostly automatically from previous course.

Back to Association Lists. On to Deep Recursion.

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:29:59 2003.
The source to the document was last modified on Thu Jan 16 13:45:59 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Outlines/outline.28.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu