Warning! You are being recorded and transcribed, provided the technology is working correctly.
Approximate optimistic overview
Scholarly
Artistic
Multicultural
Peer
Musical, theatric, sporting, and academic events involving this section’s students are welcome.
Wellness
Misc
These do not earn tokens, but are worth your consideration.
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 likemake-list
.)
We have
(vector v0 v1 v2 ...)
, which creates a vector. This is much likelist
.
We have
(vector-length vec)
, which returns the number of elements in the vector. Similar tolength
.
We have
(vector-ref vec index)
, which extracts the value at the given index. Similar tolist-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, whilelist-ref
requires us tocdr
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.
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
(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.(vector-tally-odd-helper vec (+ index 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.
(vector-increment-odd-helper! vec (+ pos 1))
.(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)
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
andtn3
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.
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
.