Skip to main content

CSC 151.01, Class 50: More Discussion of Exam 3

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Inefficient code, revisited (with problem 3)
  • Patterns (with problem 3)

News / Etc.

  • We will continue to use today’s class to discuss exam 3.
  • Continue partners
  • Grading update
    • Robert Burns remains correct.

Upcoming Work

  • NO lab writeup!
  • Reading for Wednesday: Search algorithms
    • Make sure to do the self-check!
  • Projects due TONIGHT!
  • Exam 4 distributed Wednesday.
  • No quiz Friday.

Extra credit (Academic/Artistic)

  • CS Table, TODAY, Noon: Randomness or something.
  • The Harold W. ’38 and Jean Ryan ’38 Squire Lectureship in Physics. TONIGHT, 7:00 p.m., ARH 302. Astrophysicist Kartik Sheth (Grinnell class of 1993) will present From Mumbai to Grinnell to NASA HQ - Adventures in Time and Space
  • IMPORTANT: CS Extras, Thursday, May 4, 4:15 p.m.: Inclusion in CS. Science 2022. Please attend. Snacks available at 4:00 p.m. (BONUS)
  • NEW*: 5th annual Break open the vault, 8pm, Thursday, Special Collections.

Extra credit (Peer)

  • AAA event about gender-based violance in the APIA community, Loose, 3:00-4:30 TODAY
  • NEW. This Thursday, May 4, the Grinnell College Jazz Ensemble will STOP TRAFFIC. Literally. The city (with support from Chuong Garden) has agreed to close Broad Street between 4th and 5th for our end-of-semester concert. From 7-8pm, they’ll be playing “brave music for troubled times” on the steps of the Arts Center (926 Broad), including tunes by Marvin Gaye, George Michael, Charles Mingus, Grinnell’s own Aaron Israel Levin and Kendrick Lamar. (Rain location is Sebring Lewis.)
  • Grinnell Monologues is performing on this Thursday and Friday in Main Lounge at 7:30 p.m.
  • Drill Team Show, Friday, May 5, at Triple V stables. The other side of the golf course. Free rides (cars, not pony) to the event! 6:30pm to 9:30pm. Free rides start at 6pm.
  • GSEA (Grinnell Space Exploration Agency) is launching a weather balloon on May 7th at 5:30AM. The balloon will go to almost-space and take pictures. It’ll also tell us about its location and the temperature of the air around it as it rises. We’re inviting people to come see the launch, if they want to come (despite the fact that it’s at 5:30AM).
  • Next home baseball game: Sunday, May 7 (Senior Day)
  • Advanced Performance Performances Sunday at 7pm in the Wall
  • Swim Team hosts Triathalon this weekend (running 3 miles, biking 13 miles, swimming 1/2 mile). Participate or help out. Sam will reimburse your admission fee.

Extra credit (Misc)

  • Make Your Mark on Phase 1!, Tuesday, May 2, 10 a.m., Near the Commencement Stage. (Light refreshments provided.) Leave a hand-print on the construction wall (which will become a Commencement backdrop!) to celebrate the Class of 2017. You can also make chopsticks from wood harvested at the construction site and get your photo taken at the construction-themed photo booth.
    • Need to sign up
  • May 4 town hall on belonging. 11am, May 4, JRC 101. It sounds like we have some important things to discuss; please come share your perspectives!

Other good things to do

  • Observatory open house (at the Observatory, which is at the far north end of campus. just before the country club). 8:30-10:00 p.m.

Questions

Extra credit limits?
4 peer, 4 academic/artistic, Misc count toward either.
This Thursday’s CS extra is a bonus!

Inefficient code, revisited (with problem 3)

We discovered that the following procedure requires exponential time. That is, for some lists of n elements, it requires (2^n)-1 calls. Why?

(define closest-to-zero-z
  (lambda (values)
    (cond
      [(null? (cdr values))
       (car values)]
      [(< (abs (car values)) (abs (closest-to-zero-z (cdr values))))
       (car values)]
      [else
       (closest-to-zero-z (cdr values))])))

Problem: Call (closest-to-zero-z (cdr values)) twice. Which makes it do recursive calls again and again and again. Each recursive call also does two recursive calls, so we double and double and double and double and …

This issue is why I always say “NO! Don’t do the exact same call twice.”

(define foo
  (lambda (x y)
    (square (/ (* 3 x) (sqrt y)))))
(foo (* a b c d e f g h) (* a b c d e f g h))

How might we fix this problem?

  • Use a helper procedure.
  • Let.

What would the helper procedure look like for the foo example?

(define proc
  (lambda (lst)
    (apply * lst)))
(foo (proc (list a b c d e f g h)) (proc (list a b c d e f g h)))

Nope. That still does two identical evaluations.

(define foo-dupe
  (lambda (val)
    (foo val val)))
(foo-dupe (* a b c d e f g h))

A shorter version

((lambda (val) (foo val val)) (* a b c d e f g h))

What would the let look like for the foo example?

(let ([val (* a b c d e f g h)])
  (foo val val))

What would the helper procedure look like for the closest-to-zero example?

(define closer-to-zero
  (lambda (val1 val2)
    (if (> (abs val1) (abs val2))
        val2
        val1)))

(define closest-to-zero-z
  (lambda (values)
    (cond
      [(null? (cdr values))
       (car values)]
      [else
       (closer-to-zero (car values) (closest-to-zero-z (cdr values)))])))

What would the let look like for the closest-to-zero example?

* 1
(define closest-to-zero-z
* 2
  (lambda (values)
* 3
    (cond
      [(null? (cdr values))
       (car values)]
* 4
      [(< (abs (car values)) (abs (closest-to-zero-z (cdr values))))
       (car values)]
      [else
       (closest-to-zero-z (cdr values))])))

(let ([remaining (closest-to-zero-z (cdr values))])
  ...)

Let’s try spot 3.

(define closest-to-zero-z
  (lambda (values)
    (let ([closest-in-cdr (closest-to-zero-z (cdr values))])
      (cond
        [(null? (cdr values))
         (car values)]
        [(< (abs (car values)) (abs closest-in-cdr))
         (car values)]
        [else
         closest-in-cdr]))))

Boom!

(define closest-to-zero-z
  (lambda (values)
    (cond
      [(null? (cdr values))
       (car values)]
      [else 
       (let ([closest-in-cdr (closest-to-zero-z (cdr values))])
         (if (< (abs (car values)) (abs closest-in-cdr))
             (car values)
             closest-in-cdr))])))

Yay!

Patterns (with problem 3)

How would you generalize the following procedure?

(define largest
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (max (car lst) (largest (cdr lst))))))
(define PROC
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (SOMETHING (car lst) (PROC (cdr lst))))))

How does that help us find the closest to zero?

(define closer-to-zero
  (lambda (val1 val2)
    (if (< (abs val1) (abs val2))
        val1
        val2)))

(define closest-to-zero-y
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (closer-to-zero (car lst) (closest-to-zero-y (cdr lst))))))