Skip to main content

CSC 151.01, Class 26: Vectors

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Notes on Quiz 9
    • Questions
  • Lab
  • Debrief

News / Etc.

  • Welcome to any prospective students here for Discover Grinnell!
  • Grading still isn’t done. I need less paperwork.
  • Congratulations to Singers for an excellent and moving concert.
  • Congratulations to Volleyball for making the Midwest Conference Tournament.
  • Congratulations to Men’s and Women’s Soccer for making the Midwest Conference Tournament.

Upcoming Work

Extra credit (Academic/Artistic)

  • CS Table, Tuesday at noon: TBD
  • Halloween Organ Thing, Tuesday evening in Herrick Chapel.
  • Rebirth Brass Band, Wednesday at 7:30 p.m. in Herrick.
  • CS Extras, Thursday at 4:15 p.m.: “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 kernel ([ans-so-far 1]
                 [k n])
      (if (zero? k)
          ans-so-far
          (kernel (* 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

Why do you tell us not to use quote to create lists?
If you haven’t thought deeply enough about it, you don’t get the results you expect. '(5 (sqrt 4))
For really simple lists, it’s fine. For other things, it can make your brain hurt.

Why is it that grabbing the ith element of a vector is faster than grabbing the ith element of a list?

On problem 2, can we use reverse?
No
Can we use equal?
I suppose, but char=? is better.

Lab

The correct path is “/home/rebelsky/Desktop/pg1260.txt”

Writeup: Exercise 5. Call it my-vector-fill!. Do not use the existing vector-fill!

Debrief

Why did we see inconsistent times to build a list of random elements from a vector?
Four key operations: Find the length. Choose a random number. Get that element from the vector. Add that element to a new list.
The first three should be consistent. It turns out that adding an element to a list is less consistent than you might expect. You won’t notice it for a few elements, but you may notice it for a lot.
I expect that the timer is also not perfect.
Why are the cpu times all multiples of four?
Perhaps the granularity of the timer is 1/250 of a second.
Why doesn’t my my-vector-fill! work? (What are your common “helpful”
comments.)
Remember that vector-set! does not return anything. If you want to recurse with the vector, you’ll need to do a sequence of statements.
Remember that if you use cond or when you can have multiple consequents.
Don’t create a new vector.
Reindent.