List Motions
CSC 151 - Functional Problem Solving
Leah Perlmutter | Fall 2024
Agenda for Today
Announcements
Announcements
List Motions
(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))))
(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
Before we begin
Lab time!
Lab Tips
End of lab (before quiz)
Before the quiz
As always:
Quiz time
Quiz Clarification