EBoard 32: Higher-order procedures

Warning! You are being recorded (and transcribed).

Approximate overview

  • Administrative stuff [10 min]
  • About MP8 [5 min]
  • About reading responses [10 min]
  • Questions [5 min]
  • Lab [45 min]
  • Turn in lab [5 min]

Administrative stuff

  • Happy 2^5 class!
  • Passing along a great idea from a student: When taking a SoLA, make sure to have DrRacket open with the code related to the topic.
  • Make sure to update your csc151 library for the next mini-project.
  • Don’t forget tomorrow’s Scholars’ Convocation!
  • Please fill in the mentor/tutor evaluation! (See email.)
  • Grade reports coming tonight!
  • I think I’ve caught up on all the Teams questions (except one on MP5). Let me know if you have any outstanding.
  • Don’t forget good start-of-lab habits. (Names, work habits, etc.)

Token opportunities

Academic/Scholarly

  • Wednesday, 2024-04-17, 4:15–5:30pm, Burling 1st. Book Talk by Sharon Quinsaat.
  • Thursday, 2024-04-18, 11am, JRC 101. Ruha Benjamin on “Viral Justice: How We Grow the World We Want”
  • Thursday, 2024-04-18, 4pm, JRC 2nd Floor Lobby. CS Poster Session, Part 2.
  • Thursday, 2024-04-18, 4:15–6:00pm, JRC 101. McKibben Lecture: Athenian Heroes: Re-reading the West Pediment of the Parthenon.
  • Thursday, 2024-04-18, 7pm, Science 3819 Mentor Session.
  • Tuesday, 2024-04-23, noon, some PDR. CS Table (topic TBD).
  • Tuesday, 2024-04-23, 7pm, Science 3819. Mentor Session.

Cultural

  • Friday, 2024-04-19, 4:00–5:00pm, HSSC N1170. Middle of Everywhere. (Perhaps Ghana)
  • Saturday, 2024-04-20, 2:00–5:00pm, Quad Dining Room. Japanese Spring Festival.

Peer

  • Wednesday, 2024-04-17, 5:00–6:00pm, HSSC A1231 (Kernel). GrinTECH Project Expo.
  • Saturday, 2024-04-20, Noon–3:00pm, Pioneer Park. Baseball vs. Beloit.
  • Saturday, 2024-04-20, 1:00–4:00pm, Park Street to the Bear. Carnivale.
  • Saturday, 2024-04-20, 2:30–5:00pm, Pioneer Park. Baseball vs. Beloit, revisited.
  • Saturday, 2024-04-20, 7:00–9:00pm, Herrick. Michael Londra and the Grinnell Symphony Orchestra.
  • Sunday, 2024-04-21, 10:00am–1:00pm, Tennis Courts. Men’s Tennis vs. Coe.
  • Sunday, 2024-04-21, noon–3:00pm, Pioneer Park. Baseball vs. Beloit, revisited. The Sequel!
  • Sunday, 2024-04-21, 2:00–4:00pm, Sebring Lewis Hall. Grinnell Singers with a King Singer (Simon Carrington).

Wellness

Misc

  • Saturday, 2024-04-20, 10:00am–1:00pm, Track and Field Complex. Dick Young Classic.
  • Saturday, 2024-04-20, 8:00am–5:00pm, JRC 209. Mental Health First Aid Training. Let me know how many tokens you consider appropriate for a full-day event.
  • Saturday, 2024-04-20, 11:00am–6:00pm, Mac Field. DAG Field Day. Why is DAG all caps? (Duels and Games.)

Other good things (no tokens)

  • Wednesday, 2024-04-17, 3:00–5:00pm, Softball Complex. Softball vs. Knox.
  • Saturday, 2024-04-20, 1:00–3:00pm, . Softball vs. Ripon.
  • Saturday, 2024-04-20, 3:00–5:00pm, . Softball vs. Ripon, revisited.

NO! You do not get tokens for Wonderland Harris.

Upcoming work

Mini-Project 8: Word clouds

Your last individual mini-project! (Next up is a multi-week group mini-project.)

Build word clouds.

Questions

Will using bold or italic or colors or anything other than the default font count for anything?

Nope. But you might want to use it for your own aesthetics.

Reading responses on filter

Most of you got this, or got something close. There are comments on Gradescope.

I will not go over the answer in class. Your first task on the lab is to go over it with your partner. If you can’t figure it out together, check with Maddy or me.

Some of you seem overly enamored of the pos parameter from vectors and have forgotten the techniques of list recursion. Please avoid recursive procedures that repeatedly use list-ref. It’s expensive. (So if you wrote filter using a position, rewrite without it.)

Why Sam objects (Looking ahead to next week)

Let’s consider the list-recursive version first. At each recursive step, we throw away one element using cdr. So for a list of 20 elements, we’ve called cdr approximately 20 times. For a list of 40 elements, we’ve called cdr approximately 40 times.

In contrast, consider the version with (list-ref lst pos), where pos grows from 0 to the index of the last element in the list.

A call to (list-ref lst n) requires n calls to cdr.

(define list-ref
  (lambda (lst n)
    (if (zero? n)
        (car lst)
        (list-ref (cdr lst) (- n 1)))))

Hence, if our procedure uses (list-ref lst 0) then (list-ref lst 1), then (list-ref lst 2), then (list-ref lst 3), … we’ll end up with 0 + 1 + 2 + 3 + … + 19 = 190 calls to cdr for a list of 20 elements. That’s almost ten times as many as in the basic list recursion version.

What if we have 40 elements? 0 + 1 + 2 + … + 39 = 780 calls. Almost 20 tiimes as many as the basic version! Ouch!

Note: Unlike (list-ref lst n), which requires that we call cdr n times, (vector-ref vec n) can find the element in “constant time” (independent of n).

Questions

Can we just drop an element as we go?

Yes. But then you don’t need the position. You’re always looking at the first element.

If we have to use list-ref, will it be expensive if we call list-ref in every recursive call and don’t change the list?

Yes. Try to avoid using list-ref (or (drop lst pos)) (or length) (or anything similar).

How do we avoid length?

To make sure it has exactly one element.

(define one-element
  (lambda (lst)
    (and (pair? lst)
         (null? (cdr lst)))))

(define two-elements
  (lambda (lst)
    (and (pair? lst)
         (pair? (cdr lst))
         (null? (cddr lst)))))

(define two-elements
  (lambda (lst)
    (and (pair? lst)
         (one-elment (cdr lst)))))

(define three-elements
  (lambda (lst)
    (and (pair? lst)
         (pair? (cdr lst))
         (pair? (cddr lst))
         (null? (cdddr lst)))))

(define three-elements
  (lambda (lst)
    (and (pair? lst)
         (two-elements (cdr lst)))))

(define four-elements
  (lambda (lst)
    (and (pair? lst)
         (three-elements (cdr lst)))))

Questions

Administrative

Will we get grade reports soon?

Sam intends to send them to most of you tonight.

Are we permitted to use a page of notes on quizzes?

Of course.

Can we bring one page of notes for each quiz?

I suppose so.

Looking ahead to mini-project 9: How will the groups be chosen?

You’ll see next Wednesday. You have agency.

SoLA 7

Should the neighboring pixels be a vector or a list or …?

A list.

Higher-order programming

Misc

Lab!

reduce-right

Your base case should be the singleton list, in which case you return the one value in the list.

    (reduce-right + '(1 2 3 4 5))
--> (+ 1 (+ 2 (+ 3 (+ 4 5))))
--> (+ 1 (+ 2 (+ 3 9)))
--> (+ 1 (+ 2 12))
--> (+ 1 14)
--> 15

    (reduce-right - '(1 2 3 4 5))
--> (- 1 (- 2 (- 3 (- 4 5))))
--> (- 1 (- 2 (- 3 -1)))
--> (- 1 (- 2 4))
--> (- 1 -2)
--> -3

    (reduce-right / '(1 2 3 4 5 6))
--> (/ 1 (/ 2 (/ 3 (/ 4 (/ 5 6)))))
--> (/ 1 (/ 2 (/ 3 (/ 4 5/6))))
--> (/ 1 (/ 2 (/ 3 24/5)))
--> (/ 1 (/ 2 15/24))
--> (/ 1 (/ 2 5/8))
--> (/ 1 16/5)
--> 5/16

index-of-matching!

When you are writing index-of-matching, you will almost certainly need a helper in which you keep track of the position with respect to the original list.

However, you should not use list-ref. Rather, cdr through the list as you go. The position represents how many times you’ve called cdr.