Class 22: Vectors, Continued

Back to Vectors. On to Local Bindings.

Held Monday, October 7, 2002

Summary

Today we continue our exploration of vectors.

Today's Work

Assignments

Notes on Exam 1

• I noticed a few small errors in `email-eval.ss`
• The last line of `email-value-helper` read
`(word-value (car (car histogram))))))`
and should instead read `(word-value (car (car histogram)) word-values))))`
• The line in `extract-words-helper` that read
`(cons (car chars) word-chars)`
`(cons (char-downcase (car chars)) word-chars)`
• Someone asked what I like to see for tests within the code. I like to see
• (1) A display of a note that explains the test.
• (2) A display of the Scheme code you're about to run for the test.
• (3) The test.
• (4) A comment showing the result
• For example,
```(display "Testing foo on the empty list.\n")
(display "(foo null)\n")
(foo null)
; 0
```
• Are there any other questions on exam 1?

Other Notes

• Does anyone read the class news?
• Yes, Friday's class was fun. But let's strive for fewer classes that are fun in that peculiar way.
• I'm taking Daniel (youngest) to the doctor for shots this afternoon, and won't be available in my office. I will check email this evening.

Overview

• A solution: Vectors
• Common vector procedures
• Lab

An Alternative: Vectors

• The designers of Scheme decided that it would be useful to include another compound data type that handled some of the deficiencies of lists.
• However, that meant we had to give up some of the advantages of lists.
• Vectors are compound data types that
• Have fixed size: One you create a vector, you cannot easily add elements to the vector.
• Permit indexed access: You can quickly access any element in a vector.
• Support mutation: You can change any element of a vector.
• Through the rest of the term, we'll often ask ourselves whether we're better off using a vector or a list for a particular problem.
• We've seen that lists are implemented through cons cells. How are vectors implemented? There's basically just a big cons-cell-like thing that has as many boxes as there are elements in the vector (plus a notation as to the vector's length).

Common Vector Methods

• `(vector val1 ... valn)`: a lot like `list`
• `(make-vector size element)`: make a vector of a particular length containing lots of copies of the same value.
• `(vector? value)`: determine if a value is a vector
• `(vector-length vector)`: get the length of a vector
• `(vector-ref vector pos)`: get an element from a vector.
• `(vector-set! vector pos newval)`: change part of a vector. Note the use of an exclamation point to indicate that this changes its argument.
• `(list->vector list)`: Guess
• `(vector->list vector)`: Guess
• `(vector-fill! vector value)`: Fill an existing vector with a particular value.
• Note the naming convention: A procedure that changes one of it's parameters ends with an exclamation point!

Lab

• Do the lab.
• Be ready to reflect.
• If there's time, we might even look at a sample vector procedure.

History

Thursday, 29 August 2002 [Samuel A. Rebelsky]

Monday, 7 October 2002 [Samuel A. Rebelsky]

• Filled in the details, mostly by copying uncovered material from the previous outline.
• Added introductory notes.

Back to Vectors. On to Local Bindings.

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:18 2002.
The source to the document was last modified on Mon Oct 7 07:53:29 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.22.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu