# Class 29: Vectors

Back to Deep Recursion. On to GIMP Basics.

This outline is also available in PDF.

Held: Friday, 13 October 2006

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

• Project 1 Writeup.

Notes:

• No homework during break. No reading for Monday. No coding for Tuesday. Nothing.
• Be careful: Friday the 13th falls on a Friday today.

Overview:

• Problems with lists.
• A solution: Vectors.
• 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.

## Lab

Back to Deep Recursion. On to GIMP Basics.

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 Thu Nov 30 21:43:29 2006.
The source to the document was last modified on Mon Sep 4 08:43:34 2006.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Outlines/outline.29.html`.

You may wish to validate this document's HTML ; ;

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2006 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.