EBoard 25: Vectors (Section 3)
Warning! You are being recorded and transcribed, provided the technology
is working correctly.
Approximate optimistic overview
- Administrative stuff
- About MP7
- Q&A (not on Vectors)
- Notes and questions on Vectors
Administrative stuff
Introductory notes
- I have not yet had time to get the quizzes graded. They should be graded
and available on Gradescope by Saturday night. I’ll bring the hardcopies
on Monday.
- I overslept this morning, so some links are missing from the eboard. I’ll
add them after class.
- If you sent me a recent email or TM asking for an extension, I may have
missed it. Please Teams Message me again.
Upcoming activities
Scholarly
- Thursday, 10 April 2025, 11am–noon, HSSC A1231.
Scholars’ Convocation: Jen Shook: Instead of RedFace: Relational Trails in “Indian Country,” Onstage, Online, and IRL
- Thursday, 10 April 2025, 4:00–5:30pm, the Kernel (HSSC Multipurpose Room).
CS Poster Session
- Tuesday, 15 April 2025, noon–1pm, Some PDR.
CS Table: ???
Artistic
- Friday, 4 April 2025 5:00–7:00 p.m., Saturday, 5 April 2025, 1:00–4:00 p.m.,
and Sunday, 6 April 2025, 1:00–4:00 p.m.
Educational Comics Workshop.
- You must attend all three sessions.
- But you’ll earn three tokens.
Multicultural
- Friday, 4 April 2025, 4:00–5:00 p.m., HSSC N1170 (Global Living Room).
Middle of Everywhere: Vietnam
- Friday, 4 April 2025, 6:00–9:00 p.m.,. JRC 101.
Eid Fest
- Friday, 11 April 2025, 4:00–5:00 p.m., HSSC N1170 (Global Living Room).
Middle of Everywhere: ???
Peer
Musical, theatric, sporting, and academic events involving this section’s
students are welcome.
- Read articles by your fellow CSC-151 student and comment on them online.
- Saturday at 7pm, Sunday at 2pm, a week from Thursday at 7:00 p.m. and
beyond. Stewart former-library.
Rumors, a play by Neil Simon
- Sunday, 6 April 2025 2:00 p.m. Sebring-Lewis.
Grinnell Singers
Wellness
- Friday, 4 April 2025, 6:00 p.m.–8:00 p.m., Aux Gym.
Badminton Club (Smash that bird!)
- Friday, 4 April 2025, 9:00 p.m., Noyce Elbow.
Nerf at Noyce.
- Saturday, 5 April 2025, 4:00 p.m.–6:00 p.m., Aux Gym.
Badminton Club (Smash that bird!)
- Tuesday, 8 April 2025, 12:15–12:50 p.m., GCMoA.
Yoga in the Museum.
- Tuesday, 8 April 2025, 4:30–6:30 p.m.,
BRAC P103 (Multipurpose Dance Studio).
Wellness Yoga.
- Thursday, 10 April 2025. ???. ???.
Forest Bathing.
- Tuesday, 15 April 2025, 5:00–6:00 p.m., HSSC Atrium.
Therapy Dogs.
- Tuesday, 15 April 2025, 7:15–8:15 p.m., HSSC Atrium.
Therapy Dogs.
Misc
- Friday, 4 April 2025, 3:00–5:00 p.m., Burling Digital Studio.
GCIEL / Digital Studio Workshop: Spatial Audio and Immersive Soundscapes
- Sunday, 6 April 2025, 7:30–8:30 p.m., Science 3819.
Mentor Session
- Tuesday, 8 April 2025, 7:00–8:00 p.m., Science 3820.
Mentor Session
- Wednesday, 9 April 2025, Noon–1:00 p.m., HSSC A2231 (Auditorium)
Community Forum
- “Weekly discussion on legal protections and recourse on issues
that higher education and Grinnell College face.”
- Also online.
- This week: Karen Edwards on Immigration Regulations.
Other good things
These do not earn tokens, but are worth your consideration.
- Lots of softball games this weekend.
- Saturday, 5 April 2025, Noon, Baseball field.
Baseball vs. Monmouth.
- Saturday, 5 April 2025, 2:30 p.m., Baseball field.
Baseball vs. Monmouth.
- Sunday, 6 April 2025, Noon, Baseball field.
Baseball vs. Monmouth.
Upcoming work
- Friday, 4 April 2025
- Sunday, 6 April 2025
- Monday, 7 April 2025
- Tuesday, 8 April 2025
- Submit lab writeup for Class 26 on Gradescope
- Readings
- Randomness
- Submit reading response on randomness on Gradescope
- Wednesday, 9 April 2025
- Quiz: Diagramming structures (paper only)
- Makeup quiz: List recursion
- Makeup quiz: Tracing
- Thursday, 10 April 2025
- Friday, 11 April 2025
- Sunday, 13 April 2025
- Monday, 14 April 2025
Friday PSA
- You are awesome. Please take of yourselves.
- Please be moderate in all you do.
- Consent is essential but not sufficient.
Mini-Project 7
- Key idea: Play with an underlying representation of images.
- Part one: Explore rows and columns.
- Part two: Using the idea to make variants of images.
- Part three: More variants.
- Part four: Steganography.
- Part five: Freestyle.
- You only need to do one of parts three and four for an M.
Questions
Questions
Administrative
Mini-project 6
Other
Vectors
TPS
What is a vector?
- A mechanism for storing sequential data (something like a list).
What are the key vector procedures?
(vector-set! vec index new-value)
- Go to the index in the vector
and put in new-value
. There is no corresponding list procedure (at
least not one we’ve seen).
(make-vector n val)
- Makes a vector with the given number of copies
of val
.
A lot like (make-list n val)
- Makes a list with the given number
of copies of `val.
(vector-length vec)
- Tells you how many values are in the vector.
A lot like (length lst)
.
(vector-ref vec index)
- extract the value at the given index.
(list-ref lst index)
- extract the value at the given index.
vector-ref
is generally much faster because lists require
us to call cdr
a lot of times.
(vector val0 val1 ...)
- Makes a vector of values.
(list val0 val1 ...)
- Makes a list of values.
What are the three C list procedures?
(cons val lst)
- Builds a new list by adding val
to the front of lst
.
No equivalent for vectors.
(car lst)
- Grabs the first value in lst
. We can use
(vector-ref vec 0)
to get the equivalent behavior for vectors.
(cdr lst)
- Returns a new list that contains all but the first value
of lst
.
No equivalent for vectors.
What distinguishes vectors from lists?
- Vectors are mutable; you can change the elements inside. We can’t
change the elements in lists. (We just build new, similar lists.)
- Accessing elements in lists is slow (unless we want to access them
from front to back). Accessing elements in vectors is fast.
- You can make lists shorter and longer; you can’t generally make
vectors shorter and longer.
Why would you use one rather than the other?
- We use vectors when we want to change the elements of our collection.
- We use vectors when we may need to “randomly” access the elements of
our collection.
- We use lists when we may want to change the length of our collection
(e.g., remove elements).
How does vector-set!
differ from other procedures we’ve seen?
vector-set!
is the first procedure we’ve encountered that changes
its input. This will affect how we approach programming.
- It has an exclamation point! (Also known as a “bang” because
programmers are somewhat lazy.)
- It doesn’t return anything.
- It’s easy to lose things if you don’t name them first.
> (vector-set! (make-vector 5 11) 0 2)
> (define stuff (make-vector 5 11))
> stuff
'#(11 11 11 11 11)
> (vector-set! stuff 0 2)
> stuff
'#(2 11 11 11 11)
Detour:
- You may have noted that Scheme lists and Scheme expression look
nearly the same. You can evaluate and Scheme list as if it were
a Scheme expression.
How might programming with vectors differ from the other kinds of
programming we’ve done? (Think especially about recursive programming
and/or the effects of vector-set!
.)
- We’re going to change how we sequence things.
- In the past, we’d write
(proc3 (proc2 (proc1 val)))
.
- That generally won’t work if one of the procedures returns nothing.
(define vec3 (vector-set! (vector-set! (make-vector 2 0) 0 7) 1 11))
doesn’t work.
- We will write things in sequence, rather than nesting.
(define vec3 (make-vector 2 0))
(vector-set! vec3 0 7)
(vector-set! vec3 1 11)
vec3
Some exercises
;;; (vector-tally-odd vec) -> integer?
;;; vec : (vector-of exact-integer?)
;;; Count how many odd numbers are in the vector.
(define vector-tally-odd
(lambda (vec)
(vector-tally-odd-helper vec 0)))
;;; (vector-tally-odd-helper vec index) -> integer?
;;; vec : (vector-of exact-integer?)
;;; index : exact-integer? (index in the vector)
;;; Count how many odd numbers are in the vector starting at the given
;;; index.
(define vector-tally-odd-helper
(lambda (vec index)
(if BASE-CASE
BASE-VALUE
(POST-PROCESS (vector-tally-odd-helper vec (CHANGE index))))))
- We’ll need to use recursion, but we’ll need to keep track of the
current index in order to do so.
- We’ll need to create a helper procedure.
- We’ll get to use
vector-ref
to count elements
- It will look a lot like the list recursive version
As dedicated recursive programmers, we know to ask four basic questions.
- What’s the base case? When the index is the length of the vector.
- What’s the base value? (If I start at index 5 in a length 5 vector, how
many odd numbrs can I see between index 5 and the end of the vector?).
0
- What’s the recursive call?
- What do we do with the result of the recursive call?
;;; (vector-tally-odd vec) -> integer?
;;; vec : (vector-of exact-integer?)
;;; Count how many odd numbers are in the vector.
(define vector-tally-odd
(lambda (vec)
(vector-tally-odd-helper vec 0)))
;;; (vector-tally-odd-helper vec index) -> integer?
;;; vec : (vector-of exact-integer?)
;;; index : exact-integer? (index in the vector)
;;; Count how many odd numbers are in the vector starting at the given
;;; index.
(define vector-tally-odd-helper
(lambda (vec index)
(if (>= index (vector-length vec))
0
(+ (if (odd? (vector-ref vec index))
1
0)
(vector-tally-odd-helper vec (+ 1 index))))))
> (vector-tally-odd (vector))
0
> (vector-tally-odd (vector 2))
0
> (vector-tally-odd (vector 1))
1
> (vector-tally-odd (vector 1 -1 3 11 5 1))
6
> (vector-tally-odd (vector 2 1 2 -1 3 2 2 11 2 5 2 2 1))
6
> (vector-tally-odd (vector 2 1 2 -1 3 2 2 0 0 11 2 5 2 2 1 -2))
6
;;; (vector-increment-odd! vec) -> void
;;; vec : (vector-of exact-integer?)
;;; Add one to all the odd numbers in the vector.
Q&A (from reading and elsewhere)
Can we go over how to make tn3
?
Sure.
Why is tn1
immutable?
Rules of Scheme: If you make a vector with the octothorpe, you get an immutable vector.
In our first self-check, tn1
, tn2
, and tn3
all contain the same
elements. Why is tn1
not equal to tn2
or tn3
?
tn1
is immutable. tn2
and tn3
are mutable. Immutable and mutable
structures are not equal.
What is the ‘file’ type referred to in the reading? Are we talking about an actual .rkt file here?
Whoops. Sometimes we cover how to build text and data files in the course. We didn’t this semester.
I don’t think I’m clear on what you mean by “DrRacket displaying the value of the vector” in Check 1. Does that just mean the result of calling tn1
, tn2
, etc?
Yes, displaying the value in a vector is just what you get when you type the identifier.