# Class 21: Vectors

Back to Association Lists. On to Vectors, Continued.

Held Friday, October 4, 2002

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.

Today's Work

Assignments

Notes

• I've graded homework 4.
• I've emailed each of you some notes on your assignment.
• You should read my notes on homework 4 before you turn in the exam.
• We'll look at some issues with `all-in-range`.
• We may also look at some other procedures.
• I do not plan to grade homework 3.
• I don't think you'd like me to grade it.
• When time permits, I will release some notes on it.
• Are there questions on exam 1?
• No, you don't need to use `assoc` in your implementation of `swap-name-and-phone`.
• The Putnam Mathematics Exam (a full day of challenging mathematical problems) is on Saturday, 7 December, from 9am to 5pm.
• You're guaranteed to do at least as well as I did when I took it twenty years ago.

Overview

• Problems with lists
• 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?`).

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

## History

Thursday, 29 August 2002 [Samuel A. Rebelsky]

Thursday, 3 October 2002 [Samuel A. Rebelsky]

• Added the short section on `begin`.
• Other minor changes.

Friday, 4 October 2002 [Samuel A. Rebelsky]

• Added introductory notes.

Monday, 7 October 2002 [Samuel A. Rebelsky]

• Removed uncovered sections.

Back to Association Lists. On to Vectors, Continued.

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 Mon Dec 2 08:41:16 2002.
The source to the document was last modified on Mon Oct 7 07:52:04 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.21.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu