1 of 16

List Motions

CSC 151 - Functional Problem Solving

Leah Perlmutter | Fall 2024

2 of 16

Agenda for Today

  1. Announcements
  2. List Motions
  3. Lab
  4. Quiz

3 of 16

Announcements

4 of 16

Announcements

  • Leah not doing so well because somebody I care about lost a family member this week
  • Setting expectations for evening tutoring sessions

5 of 16

List Motions

  • nested pair structure of a list
  • Self check 1 (demo code on following slides)

6 of 16

7 of 16

8 of 16

(test-case "positive edge case: replace first element"

equal?

(list 999 2 3)

(lambda ()

(replace-first 9 999 (list 9 2 3))))

(test-case "negative case: replace something not in the original list"

equal?

(list 1 2 3)

(lambda ()

(replace-first 9 999 (list 1 2 3))))

(test-case "negative edge case: empty list"

equal?

(list)

(lambda ()

(replace-first 9 999 (list))))

9 of 16

(define replace-first

(lambda (x y lst)

(match lst

[null null]

[(cons head tail)

(if (equal? head x)

(cons y tail)

(cons head (replace-first x y tail)))])))

;[(cons x tail)

; (cons y tail)]

;[(cons head tail)

; (cons head (replace-first x y tail))])))

; (cons z (replace-first x y tail))])))

;; Hypotheses

;; on line 12, we can't cons z, it's undefined

;; on line 9, x is not what we think it is

;; on line 9, x is being locally rebound

;; line 9 is no the right way to make sure head is x

;; we could use cond instead

10 of 16

Before we begin

  • Suggested Ice Breaker:
    • What is a really good or really bad food you ate this week?

11 of 16

Lab time!

12 of 16

Lab Tips

  • Every recursive function has
    • Base Case
      • solve the whole problem in one step
    • Recursive Case
      • make the problem smaller and recur
  • Think Before You Code!
    • Use mini-whiteboards or a piece of paper to sketch out a plan!

13 of 16

End of lab (before quiz)

  • 5 minutes left -- make a plan to finish your lab exercises!
  • Before submitting, make a comment in your lab code saying how far you got through the exercises
  • To prepare for the quiz, log out of your lab computer and put away everything except a writing implement (pencil and eraser recommended)

14 of 16

Before the quiz

  • Please log out of your lab computer and put away everything except a writing implement (pencil and eraser recommended)
  • You have 15 minutes for the quiz, modified by any accommodations that you have talked with me about
  • When finished, hand it in at the front of the room
  • You may leave when finished
  • If you have to talk to Prof. Perlmutter, we can go outside

As always:

  • check course website for assignments and due dates
  • When you go, put note cards back in bin if you have not already

15 of 16

Quiz time

16 of 16

Quiz Clarification

  • Quiz goes until 4:00 (we started late)