Skip to main content

Class 38: Vectors

Held:

We consider *vectors, an alternative to lists for storing collections of data.*

Preliminaries

Overview

  • Some problems with lists
  • A solution: vectors
  • Behind the scenes: How Scheme implements vectors
  • Important vector procedures

Updates

News / Etc.

  • Continue partners!
  • Exam 2 returned.
  • Current grades distributed.
  • Quizzes 7-9 returned (sorry for the delay).
  • Answer key for exam 2 forthcoming.
  • Our graders are working on the writeups.

Upcoming Work

  • Lab writeup for class 37: Exercise 3 from the Pairs lab
  • Lab writeup for class 38: Exercise 2 from the Vectors lab
  • Reading for Wednesday: Trees
  • HW7 is due tonight!
  • Exam 3 will be distributed tomorrow.
    • Yes, we worked on making it more reasonable.

Extra credit (Academic/Artistic)

  • CS Table, TODAY, noon, Technical Interviews
  • CS Extras, Thursday, 4:15 pm, Ursula Wolz on High School Coding Conferenc3
  • The Barber of Seville, Monday, 17 April 2017, 7:30 p.m., Herrick
  • Spark Tank Pitch Contest, Thursday, 11am-1pm, Harris
  • Jonathan Katz: Donald Trump, Hilary Clinton, and the Politics of Presidential Masculinity, Thursday, 7:00 p.m., Harris Cinema.
  • James McBride and the Good Lord Bird Band, Thursday, 8pm, Herrick

Extra credit (Peer)

  • Submit to (or attend) the Art House Arts Fest on April 22nd. They want visual art, performance art, musical talent, crafting skills, culinary arts etc. If anyone is interested in participating they can email our Art House member.
  • After spending this entire weekend doing Improv at a conference, Ritalin Test Squad will be doing 24 Hour Improv the upcoming Friday. They will be performing in Loose Lounge from 6pm Friday to 6pm Saturday. There may be other improv troupes performing with them.
    • Get sleep! Do not attend for all 24 hours.
  • Baseball vs. Knox Saturday at noon or 2:30 p.m.
  • Drag Show Saturday.

Extra credit (Misc)

None right now.

Other good things to do

Self care.

Data Types

  • At the start of the semester, we decided that “basic values and operations on those values” are key to writing algorithms.
    • We tend to use the word “type” to express these two concepts.
  • We’ve seen a variety of characteristics of types in 151.
    • Some types are defined in terms of a list of possible values or a simple construction method: Character, Boolean, RGB color, etc.
    • Some types that can potentially be defined recursively: Drawings-as-value, Maybe integers, Lists.
    • Some types are designed to collect other kinds of values: Lists. (Okay, maybe that’s it for now.)
  • We’re about to learn a few more. Vectors today. Pair structures (particularly trees) on Monday.

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.
    • (vector-length *vector*)

Implementing Vectors

  • To be done live in class: Memory layout and more.

Lab