EBoard 18: Recursion (Section 1)

Warning! You are being recorded and transcribed, provided the technology is working correctly.

Approximate optimistic overview

  • Quiz
  • Administrative stuff
  • Q&A
  • Recursion basics
  • Examples!
  • Converting to Scheme

Administrative stuff

Introductory notes

  • I’ve now had a conversation with a few people about being more efficient. The first step is something like “Ask for help when something doesn’t work; move on to another problem if help is not immediately available.”
    • Remember the Zone of Proximal Development
  • When you ask me for help, it helps if you send me code (e.g., your whole Racket file). It may seem that way sometimes, but I’m not psychic.
  • If you are leaving early for Spring Break, please drop me a note. Otherwise, I expect you to be in class on Friday.
  • There’s no lab today. We’ll have a combination of lecture and discussion.

Upcoming activities

Scholarly

  • Thursday, 6 March 2025, 11:00 a.m.–Noon, JRC 101. Scholars’ Convocation: Lisa Mueller: Most Protests Fail. It Doesn’t Have to Be That Way

Artistic

Multicultural

  • Friday, 28 March 2025, 4:00–5:00 p.m., HSSC N1170 (Global Living Room). Middle of Everywhere: ???

Peer

Musical, theatric, sporting, and academic events involving this section’s students are welcome.

Wellness

  • Tuesday, 26 March 2025, 12:15–12:50 p.m., GCMoA. Yoga in the Museum.
  • Tuesday, 26 March 2025, 4:30–6:30 p.m., BRAC P103 (Multipurpose Dance Studio). Wellness Yoga.
  • Friday, 28 March 2025, 6:00 p.m.–8:00 p.m., Aux Gym. Badminton Club (Smash that bird!)
  • Friday, 28 March 2025, 9:00 p.m., Noyce Elbow. Nerf at Noyce.
  • Saturday, 29 March 2025, 4:00 p.m.–6:00 p.m., Aux Gym. Badminton Club (Smash that bird!)

Misc

  • Sunday, 9 March 2025, 7:30–8:30 p.m., Science 3819. NO Mentor Session
  • Sunday, 23 March 2025, 7:30–8:30 p.m., Science 3819. Mentor Session (I think)
  • Tuesday, 25 March 2025, 7:00–8:00 p.m., Science 3820. Mentor Session

Other good things

These do not earn tokens, but are worth your consideration.

Upcoming work

Questions

We’re skipping recursion questions for now.

Administrative

When can I make up the Documentation LA?

On SoLA 2. (or SoLAs 3, 4, or 5)

Suppose I don’t do well on the lists LA. When can I make that up?

On SoLA 2. (or SoLAs 3, 4, or 5)

MP5

I’m struggling with how to get part 2 to work. Any hints?

You’ll need to use some of the same ideas as in part 1. In particular, you’ll find it helpful to write a helper procedure that checks whether its parameter is a shape or not and does different things based on that test.

This also applies for part 3.

The autograder is borken.

I’ll work on fixing that today.

On the freestyle, how many procedures should we write?

I’d prefer that you write at least four procedures.

One will mimic part one. (Also helpers.)

One will mimic part two. (Also helpers.)

One will mimic part three. (Also helpers.)

One will tie everything together.

Scheme

Other

I have a complicated private question. Should I ask it in class?

You can Teams Message me.

Recursion basics

  • Recursion is an algorithm design technique.
  • It bears some resemblance to decomposition.
    • In decomposition, we break the problem into smaller problems.
    • In recursion, we (generally) break the input into a smaller input.
  • To write your procedure, assume you already have a procedure that does exactly the same thing, but only for smaller inputs.
  • Identify a case in which the input is simple enough that you can solve it without a helper procedure.
  • With these two parts in place, your procedure will generally look something like the following.
if the input is simple enough to solve directly
  solve it directly
otherwise
  make the input a little bit smaller
  solve the problem with the smaller input USING EXACTLY THE SAME
    PROCESS
  update that solution with the element (or elements) we removed to
    make the input smaller.

There’s a magic recusion fairy who makes this work.

Examples!

How many cards are in this stack of cards?

if there's nothing left in the stack of cards
  we have zero cards
otherwise
  remove one card
  ask the same question of your assistant (the input is smaller so we
    meet UGSDW regulations)
  add 1 and return the result

We’ve written length.

How many cards in this stack have the same number of 1’s and 0’s.

if there's nothing left in the stack
  return 0
otherwise
  remove one card
  ask the same question of your assistant
  if you have the same number of 1's and 0's on that removed card
    add 1 to the result you got from your assistant
  otherwise
    just pass along the result you got from your assistant.

We’ve written tally.

Please give me the cards that have consonants

if there's nothing left in the stack
  return nothing
otherwise
  remove one 
  ask your assistant to do the same task
  if your card is a consonant
    add that card to what you got from your assistant and return the
      modified stack
  otherwise
    pass along what your assistant gave you

We’ve written: filter

Converting to Scheme

Things we need to think about

  • How do we check if the list is empty?
  • How do we add 1?
  • How do we add something back to what our assistant returned?
(define my-length
  (lambda (lst)
    (if (null? lst)
        0
        (+ 1 (my-length (cdr lst))))))

We got it right on the first try!

Note: If we don’t simplify the parameter, we’ll like get a giant error box and our computer might catch on fire.

(define tally-odds
  (lambda (lst)
    (if (null? lst)
        0
        (if (odd? (car lst))
            (+ 1 (tally-odds (cdr lst)))
            (+ 0 (tally-odds (cdr lst)))))))

We got it right on the first try! (but with a bit of prompting)

But … adding zero is pointless.

(define tally-odds
  (lambda (lst)
    (if (null? lst)
        0
        (if (odd? (car lst))
            (+ 1 (tally-odds (cdr lst)))
            (tally-odds (cdr lst))))))

But … nested ifs should be replaced by conds.

(define tally-odds
  (lambda (lst)
    (cond
      [(null? lst)
       0]
      [(odd? (car lst))
       (+ 1 (tally-odds (cdr lst)))]
      [else
       (tally-odds (cdr lst))])))
(define select-evens
  (lambda (lst)
    (cond
      [(null? lst)
       null]
      [(even? (car lst))
       (cons (car lst) (select-evens (cdr lst)))]
      [else
       (select-evens (cdr lst))])))

Algorithms review

At the beginning of the semester, we said that we had to know how to do six tings in order to write algorithms. What were they? And what have we learned about them in Scheme?