CSC 151.01, Class 38: Vectors
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- A bit more on pairs
- A bit about vectors
- Finish pairs lab
- Start vectors lab
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.
Friendly reminders
- Please try to do the reading in advance of class.
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.
- Basseball 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.
Questions
- Should we teleport the turtle in our
turtle-snowflake!orturtle-tree! - procedures?
- No. Both should draw the appropriate structure wherever the turtle is, and in whatever direction the turtle is facing. That’s one of the normal policies of turtle procedures.
turtle-snowflake!should also return the turtle to its original position and orientation. (That should happen naturally if you write the procedure correctly.)- Can you give us a sense of how we should format question 3?
- Sure.
Go forward by length
Figure out angles
Recurse for each angle
turn back to original orientation
turn around
go forward by length
turn around
- What happens with clones?
- The clone has the same position, orientation, color, and pen.
- The clone and the original now work independently.
- Will you give me extra credit for asking permission?
- Sure. This time only. Email me to remind me.
- Should we complete each branch before going on to the next?
- Yes, your life will be much easier that way.
A bit more on pairs
- Whenever you call cons, directly or indirectly, Racket builds a two element “cons cell”
- Each element of the cell can contain either
- A link to something (another cons cell, a value)
- Null
- We tend to draw cons cells as contiguous. However, the pairs or cons cells get scattered throughout memory.
- We teach you about pairs and cons cells to help you think about costs.
- We teach you about pairs and cons cells because they can ehlp you think about more sophisticated structures.
- When you are drawing or thinking about pairs and lists and such, note that a list of N elements will have a “spine” of N cons cells.
- If the last element is null, you’ll have a null in the left side of the last cons cell.
A bit about vectors
Sometimes it’s useful to
- Index: Quickly get to the middle of a collection (or, more generally, the ith element)
- Mutate: Be able to change elements in the middle
Scheme provides something called a vector. Like a list, but
- They look diffeent in that the start with a pound sign (hash, octothorpe)
- They are indexed in a way that it’s quick to access the ith element. (Lists are also indexed, but it’s slow to access the ith element)
- You can’t really change a value in a list, you can only make a new list. However, you can change a value in a vector.
- Lists generally change size at will (by adding at the front or cdring). Vectors have fixed size.
Similar to lists
- Collect elements
- If we use a map-like thing, elements need to correspond
Recursion
- Lists: Keep cdring
- Vectors: Keep track of position and keep incrementing until we reach the lenght.
Key operations
- Index:
(vector-ref vec pos) - Mutuate:
(vector-set! vec pos newval) - Create:
(make-vector len val)or(vector val1 val2 ... valn) - Length:
(vector-length vec)
Finish pairs lab
Or at least problems 2 and 3.
Note: Each cons corresponds to one cons cell (pair). Each pair corresponds to one cons cell. You should therefore have equal numbers of cons cells in your diagram and calls to cons in your expression (or implicit expression).
(cons 'a null)-> one cons cell(list 'a 'b)-> short for(cons 'a (cons 'b null))-> two cons cells(cons 'a 'x)-> one cons cell
Once you finish problem 3 of pairs lab, go on to vectors.
Write up pairs lab
Write up exercise 3 from the lab on pairs and pair structures.
Hand the sheet of paper with the diagrams, with your names on it, to SamR.
Vectors lab
- What’s going on in problem 1?
aardvarkis a list of the values 1, 2, 3, 4chinchillais a vector of the values 1, 2, 3, 4- So they are similar
- We are exploring what happens if we modify them.
- Can we really modify lists as in the following?
- It appears that we can create a new list with the same name.
> (define aardvark (list 1 2 3 4))
> (define aardvark (cons 0 (cdr aardvark)))
> aardvark
'(0 2 3 4)
- Will that work in the definitions pane?
- No. DrRacket thinks that redefining something is generally dangerous, and will tell you not to do it in the definitions pane.
- Can you suggest a general pattern for recursion over vectors?
- There are lots of general patterns. Maybe we’ll look at some tomorrow.
Write up vectors lab
Write up exercise 2 from the lab on vectors.
Send your solution to csc151-01-grader@grinnell.edu.
Title your email CSC 151.01 Writeup for Class 38 (YOUR FULL NAMES).