EBoard 25: Vectors (Section 1)

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
  • Lab

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 at the start of lab.
  • 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.
  • Sunday, 6 April 2025 2:00 p.m. Sebring-Lewis. Grinnell Singers

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 students and comment on them online.
  • 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.

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.

Upcoming work

Friday PSA

  • You are awesome (even if you’re not here). Please take care 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

Introductory comments

TPS

What is a vector?

A way to create/display/collect a set of values.

It has new ways to work with the set when compared to lists as well as some similarities.

We have (vector-set! vec index new-value), which changes one value in the vector. This seems to be different than what we have for lists.

We have (make-vector length val), which makes a vector of the given length containing that many copies of the value. (This is much like make-list.)

We have (vector v0 v1 v2 ...), which creates a vector. This is much like list.

We have (vector-length vec), which returns the number of elements in the vector. Similar to length.

We have (vector-ref vec index), which extracts the value at the given index. Similar to list-ref.

We also have an equivalent to the tick mark, '#(v0 v1 ...).

What distinguishes vectors from lists?

We have vector-set! which mutates (changes) the vector. We do NOT have a way to change lists.

vector-ref is supposedly fast, while list-ref requires us to cdr through the whole list. (And is somtimes slow.)

We don’t seem to have an append for vectors.

The three C’s of lists are car, cdr (not for vectors), cons (not for vectors). You can easily change the length of a list (or, more precisely, build a new list of a different length); you cannot easily change the length of a vector.

Why would you use one rather than the other?

If our work involves adding and removing elements, lists are probably better.

If our work involves changing elements or “random access” to elements, vectors are probably better.

How does vector-set! differ from other procedures we’ve seen?

It doesn’t return anything. We’re used to using procedures that return things.

We can’t do something like (vector-set! (vector-set! (make-vector 2) 0 7) 1 11)

It becomes much more important to name things.

How might programming with vectors differ from the other kinds of programming we’ve done? (Think especially about recursive programming and maybe vector-set!.)

In the past we nested for sequencing. (proc3 (proc2 (proc1 ...)))

Now, we have to sequence in a different way.

(proc1! ...)
(proc2! ...)
(proc3! ...)

That is, we don’t generally nest ! procedures.

Some exercises

Both of these will require a recursive helper that keeps track of the index.

;;; (vector-tally-odd vec) -> integer?
;;;   vec : (vector-of exact-integer?)
;;; Count how many odd numbers are in the vector.

Things to think about

  • We’re going to have to keep track of where in the vector we are.
  • That suggests a helper procedure.
  • We’ll be recursing over the position, starting at 0. Think about a base case, a base case value, a recursive call, and what to do with the recursive call.
    • base-case. Let’s consider (vector 5 8 9 2 3 3 7). Observation: We’re done either when we’re at length-1 (that’s the last element) or length (that’s beyond the last element). I’ll go with length.
    • base-case-value: 0
    • recursive-call (vector-tally-odd-helper vec (+ index 1)).
    • what to do with the recursive call: If the current thing is odd, add 0, otherwise, add 1.
(define vector-tally-odd
  (lambda (vec)
    (vector-tally-odd-helper vec 0)))

;;; (vector-tally-odd-helper vec index) -> integer?
;;;   vec : vector-of? exact-integer?
;;; Count how many odd values are in the vector starting at position `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 (+ index 1))))))

(define vector-tally-odd-helper
  (lambda (vec index)
    (cond
      [(>= index (vector-length vec))
       0]
      [(odd? (vector-ref vec index))
       (+ 1 (vector-tally-odd-helper vec (+ index 1)))]
      [else
       (vector-tally-odd-helper vec (+ index 1))])))
> (vector-tally-odd (vector))
0
> (vector-tally-odd (vector 0))
0
> (vector-tally-odd (vector 1))
1
> (vector-tally-odd (vector 1 2 3 4 5 6))
3
> (vector-tally-odd (vector 4 1 2 3 4 5 6 6 8 2 4))
3
> (vector-tally-odd (vector 4 1 2 3 4 5 6 6 8 2 4 5))
4
> (vector-tally-odd (vector 4 1 2 3 4 5 6 6 8 2 4 5 7))
5
;;; (vector-increment-odd! vec) -> void
;;;   vec : (vector-of exact-integer?)
;;; Add one to all the odd numbers in the vector.
;;;
;;; E.g.,
;;;   > (define vec (vector 1 2 3 4 5))
;;;   > (vector-increment-odd! vec)
;;;   > vec
;;;   > #(2 2 4 4 6)
(define vector-increment-odd!
  (lambda (vec)
    (vector-increment-odd-helper! vec 0)))

;;; (vector-increment-odd-helper! vec index) -> void
;;;   vec : vector-of exact-integer?
;;;   index : exact-integer? (in the appropriate range)
;;; Add one to all the odd numbers in the vector starting at the
;;; the given index and going to the end.
  • Thought processes
  • Recursion
    • base-case-test: When the index is greater than the length.
    • base-case-action: Return nothing.
    • recursive-case: (vector-increment-odd-helper! vec (+ pos 1)).
    • pre or post-process of recursive case. Check if the current value is odd or even and, if it’s odd, add 1 to that value. Otherwise, add 0 to that value.
  • Also:
    • How do we deal with the “add one to odd numbers”?
    • (increment (vector-ref vec index))
(define vector-increment-odd-helper!
  (lambda (vec index)
    (cond
      [(>= index (vector-length vec))
       (void)]
      [else
       (when (odd? (vector-ref vec index))
          (vector-set! vec index (increment (vector-ref vec index))))
       (vector-increment-odd-helper! vec (+ index 1))])))
> (define vec (vector 1 2 3 4 5))
> vec
'#(1 2 3 4 5)
> (vector-increment-odd! vec)
> vec
'#(2 2 4 4 6)

Q&A (from reading and elsewhere)

Can we go over how to make tn3?

Sure.

(define tn3 (make-vector 2))
(vector-set! tn3 0 7)
(vector-set! tn3 1 11)
(define tn3 (make-vector 2 7))
(vector-set! tn3 1 11)

Why is tn1 immutable?

(define tn1 #(7 11))

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.

Lab

Note: We’ll do a second vector lab on Monday.

Note: This uses randomness, which we don’t officially learn until next week. For now, assume that random means unpredictable.