This class will be recorded! Its use is limited to members of the class. Please do not share with others.
Approximate overview
Attend (or watch recording) and send a one-paragraph reflection.
Will we get cut from 161?
Nope.
Do we have to retake quizzes we got points for?
No.
The add-to-end
and append-lists
procedures from the last lab
seemed like a lot of work to do a simple thing. Is there a better
way?
In terms of the underlying list model in Scheme: No.
In terms of what you have to write: Yes.
(reverse (cons x (reverse l)))
To deal with the end of the list, you have to recurse through the list.
I was using an if
for the self-check. What are other options?
Lab.
Could you explain pattern matching?
There are situations in which you would probably write a cond
and
each aspect of the cond
would depend on the structure of some value.
(define add-to-end
(lambda (lst val)
(cond
[(null? lst)
(list val)]
[(not (list? lst))
(error "That's not a list")]
[else
(cons (car lst) (add-to-end (cdr lst) val))])))
When our cond is basically structural, we can use match
instead.
(define add-to-end
(lambda (lst val)
(match lst
['()
(list val)]
[(cons x xs)
(cons x
(add-to-end xs val))]
[something-else
(error "Not a list")])))
The other nice thing about match
is that it allows us to name the
parts of something. (See the x
and xs
above.)
Intuition
“A piecewise function”
> (define !
(lambda (n)
(match n
[0 1]
[other (* n (! (- n 1)))])))
> (! 0)
1
> (! 6)
720
I get the meaning but not the syntax.
Practice.
Some notes
(foo (bar 3))
; I expect to see 42.”(check-equal? (foo (bar 3)) 42 "Life, the Universe, and Everything")
(sublist '(A B C D) 2 4)
; I expect to
see '(B C)
.”(check-equal? (sublist '(A B C D) 2 4) '(B C) "Middle of four-element list")
check-equals?
(or whatever) tests immediately below
the procedure definition.Consider the following (unimplemented) procedure.
;;; (sublist lst start finish) -> list-of any? ;;; lst : list-of any? ;;; start : integer? ;;; finish : integer? ;;; Grab the sublist of lst starting at start and finishing ;;; immediately before finish. ;;; ;;; Prereq: 0 <= start <= finish <= (length lst)
Write five or more tests of sublist, including at least two “normal cases” and at least two “edge cases” or “corner cases”.
Use the simple
check-equal?
format.
The syntax is
(check-equal? expression expected note)
. It’s fine if you don’t get it quite right. ```
One example
(check-equal? (sublist (list "A" "B" "C") 1 2)
(list "B")
"Single element of a list of strings")
Things Sam saw
sublist
Your goal. Come up with one “normal” case and one “edge” case that you think will be (a) useful and (b) different than what others are posting. I will likely call on you.
(check-equal? (sublist (map thing (list "red" "yellow" "green" "blue" "purple")) 4 5)
(list (thing "purple"))
"Shapes, end of list")
(check-equal? (sublist (list #\a #\b 0.5 3.0) 0 1)
'(#\a)
"Multiple types, looking at the start of the list")
(check-equal? (sublist (list 1 2 3 7 5 43 4 5) 1 3)
'(2 3)
"Normal case 1")
(check-equal? (sublist (list 0 0 0 0 0) 0 1)
'(0)
"Start of list; zeros mess stuff up")
(check-equal? (sublist '(8 7 5 4) 2 3)
'(5)
"Normal case, looking for single element")
(check-equal? (sublist '(8 7 5 4) 2 2)
'()
"Empty list, taken from the middle")
(check-equal? (sublist (list "a" "b" "c") 0 0)
'()
"start and finish are the same")
(check-equal? (sublist '() 0 0)
'()
"starting with the empty list")
(check-equal? (sublist (range 20) 0 20)
(range 20)
"Whole list for a large list")
How many tests should we write?
It depends on the situation.
Enough that you would stake your grade (on the assignment?) on the correctness of the procedure.
As many as Sam says.
You should know the drill by now.
You will just access the .rkt
files directly today (at least I
hope you will).