CSC 151.03, Class 26: Vectors
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Notes on Friday’s quiz
- Questions
- Lab
- Debrief
News / Etc.
- Welcome to any prospective students!
- Grading still isn’t done. I need less paperwork.
- Congratulations to Men’s and Women’s Soccer for making the Midwest Conference Tournament.
Upcoming Work
- Writeup for class 25 due Monday at 10:30 p.m.
- Exercises 1 and 3.
- Hand to: Sam (or under Sam’s door)
- Writeup for class 26 due Wednesday at 10:30 p.m.
- Exercise 6. No documentation necessary.
- To: csc151-03-grader@grinnell.edu
- Subject: CSC 151.03 Writeup 26 (YOUR NAMES)
- Assignment 6 due TOMORROW.
- Read Binary trees for Wednesday’s class.
- Yes, it’s ready!
Extra credit (Academic/Artistic)
- CS Table, Tuesday at noon: The Google Memo.
- Rebirth Brass Band, Wednesday at 7:30 p.m. in Herrick.
- CS Extras, Thursday at 4:15 p: “Building your own programming language and other reasons to go to graduate school”
Extra credit (Peer)
Extra credit (Misc)
Other good things
Notes on Quiz 9
Two common problems: Named let and tail-recursive tally.
Named let
Original:
(define factorial
(lambda (n)
(fact-helper 1 n)))
(define fact-helper
(lambda (ans-so-far k)
(if (zero? k)
ans-so-far
(fact-helper (* k ans-so-far)
(- k 1)))))
Ideas of named let
- Put the helper inside the primary procedure; others don’t need it.
- Put the parameters and the initial values together.
- (Strange syntax)
- The body of the helper should not change, except that you may change the name of the helper.
Form
(let NAME ([PARAM-1 INITIAL-1]
[PARAM-2 INITIAL-2])
BODY)
Revised
(define factorial
(lambda (n)
(let colonel ([ans-so-far 1]
[k n])
(if (zero? k)
ans-so-far
(colonel (* k ans-so-far)
(- k 1))))))
- (Eventually, you will just write the helpers within the procedure, rather than write them separately and move them within the procedure.)
Tail-recursive list tally
- Idea: We use a helper to keep track of both the tally and what we have left to explore.
- When we’re done, the tally should be done; no need to backtrack.
Chart
tally remaining
----- ---------
0 '(5 -2 1 4 3 -7)
1 '(-2 1 4 3 -7)
1 '(1 4 3 -7)
2 '(4 3 -7)
3 '(3 -7)
4 '(-7)
4 '()
Code
(define tally-positive
(lambda (lst)
(let kernel ([tally 0]
[remaining lst])
(cond
[(null? remaining)
tally]
[(positive? (car remaining))
(kernel (increment tally) (cdr remaining))
[else
(kernel tally (cdr remaining))]))))
Questions
- What does the ! mean in the procedure name?
- We’re calling the procedure not for a return value, but because we want to change one of the parameters.
- Things without ! generally don’t change their parameters, which means we call them to get back a new value.
A vector example
> (define vec (vector "a" "vector" "of" "five" "elements"))
> vec
'#("a" "vector" "of" "five" "elements")
> (vector-set! vec 3 "many")
> ; Look! It didn't return anything.
vec
'#("a" "vector" "of" "many" "elements")
A list example
> (define phrase (list "a" "list" "of" "five" "elements"))
> phrase
'("a" "list" "of" "five" "elements")
> (append (take phrase 3) (list "many") (drop phrase 4))
'("a" "list" "of" "many" "elements")
> phrase
'("a" "list" "of" "five" "elements")
Lab
- Hints on
vector-fill! - Remember that
vector-set!returns nothing. You can’t use the return value fromvector-set!as the parameter to a procedure. - You are changing the same vector at each recursive call. There’s no need to make it a parameter to the kernel.
- You may need to sequence operations. Remember that both
whenandcondlet you have a sequence of consequents.
Writeup: Exercise 6, No documentation required. This is class meeting 26.
Debrief
- What is
gc? - Garbage collection. The time spent trying to find things you’ve created but aren’t using any more and to free the resources associated with them.
- Why is the time inconsistent?
- You’re randomly choosing which elements of a list to take. Those near the front will be quicker than those near the end.
- What about when I’m using vectors?
- You’re still building a list. It turns out that the time to allocate space to build one pair is not consistent.