EBoard 33: Tail recursion

Warning! You are being recorded (and transcribed).

Approximate overview

  • Quiz [15 min]
  • Administrative stuff [5–10 min]
  • Questions [5–10 min]
  • Lab [40–45 min]
  • Turn in lab [5 min]

Administrative stuff

  • Welcome to any prospective students attending today!

Token opportunities

Academic/Scholarly

  • Tuesday, 2024-04-23, noon, some PDR. CS Table (topic TBD).
  • Tuesday, 2024-04-23, 7pm, Science 3819. Mentor Session.
  • Tuesday, 2024-04-25, 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

  • 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 (or Carnival)
  • 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)

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

Upcoming work

Friday PSA

  • It’s Friday.
  • There are many great things to do this weekend!
  • Most can easily be enjoyed without altering yourself.
  • Be good about Alice.
  • Please take care of yourselves.
  • Please take care of others.
  • Consent is essential.

Questions

Administrative

Do we have to be in a group for Project 9?

No. The amount of work expected on the final project is proportional (more or less) to the number of people in the group.

We’ll talk more about that on Wednesda.

MP 8

SoLA 4

Tail recursion

Where does the term “kernel” come from?

My colleague, John Stone, seems to have coined it as part of a broader Iowan “husk and kernel” programming strategy. The husk protects the kernel. The kernel is where the real value happens.

Is there a situation where you could have multiple accumulator variables?

Certainly. The split procedure that you worked on in the last lab is one such example.

What is letrec?

letrec is just like let, but is necessary when you define recursive procedures.

As you may recall, (let ([name exp]) ...), evaluates exp and then binds it to name. For a recursive procedure, exp will include name. But name may not be bound yet. letrec does something a bit more complicated to accommodate this situation.

Sam’s stupid demo.

(define fun
  (lambda (x)
    "happy happy joy joy"))

(define hssc
  (lambda (x)
    (let ([fun (lambda (x)
                 (if (zero? x)
                     "done"
                     (string-append "something " (fun (- x 1)))))])
      (fun x))))

(define husk
  (lambda (x)
    (letrec ([fun (lambda (x)
                    (if (zero? x)
                      "done"
                      (string-append "something " (fun (- x 1)))))])
      (fun x))))

[Sam told an amazing shaggy dog story leading up to “unbounded fun.]

When using a tail-recursive kernel with an extra so-far parameter, can you make decisions on how you change so-far at each step?

Certainly. You can do whatever you want within the recursive call (as long as you avoid infinite recursion).

Misc

Lab!

Exercise 3

Which of the following do you prefer (for the original)?

(define tally-symbols
  (lambda (lst)
    (if (null? lst)
        0
        (if (symbol? (car lst))
            (+ 1 (tally-symbols (cdr lst)))
            (tally-symbols (cdr lst))))))

or

(define tally-symbols
  (lambda (lst)
    (cond
      [(null? lst)
       0]
      [(symbol? (car lst))
       (+ 1 (tally-symbols (cdr lst)))]
      [else
       (tally-symbols (cdr lst))])))