This class will be recorded! Its use is limited to members of the class. Please do not share with others.
Approximate overview
tally-value [~20 min]Events
I’m not sure if all of these links are correct. Let me know if any are not.
Why did my tokens disappear?
Gradescope is evil. Perhaps more evil than Sam.
What’s our goal?
Take numbers (or other info) you come up with and use them as characteristics of the images you generate.
This text is retained from yesterday’s class.
We’re using tally or tally-value a lot on this procedure. Let’s
explore how it might work, or at least how we might trace it conceptually.
We’ll assume that it has to work step-by-step, looking at each value
in turn.
(tally-value '(b a c a b a) 'a)
; The b is not the same as a, so it won't contribute to our overall
; tally. Throw away.
; We should go on to the next thing
--> (tally-value '(a c a b a) 'a)
; Compare the a to a. They are the same.
; Add 1 to the count
; And then move on to the rest of the list
--> (+ 1 (tally-value '(c a b a) 'a))
; Compare c to a. No. Continue on
--> (+ 1 (tally-value '(a b a) 'a))
; Compare a to a. Same. Add 1 and continue
--> (+ 1 (+ 1 (tally-value '(b a) 'a)))
; Compare b to a. Different. Throw away the b and continue
--> (+ 1 (+ 1 (tally-value '(a) 'a)))
; Compare a to a. Same. Add 1 and continue
--> (+ 1 (+ 1 (+ 1 (tally-value '() 'a))))
; There are 0 a's in the empty list. We're done.
--> (+ 1 (+ 1 (+ 1 0)))
--> (+ 1 (+ 1 1))
--> (+ 1 2)
--> 3
If we were using strings and wanted to be case insensitive, would it be different?
Our trace would be similar.
E.g.,
(tally? (section string-ci=? <> "Eh") list-of-words)would require that we ask “Is the first word equivalent to “Eh”, ignoring case?” rather than “Is the first element ‘a?”
Our code would be somewhat diffeent.
Can we do more practice tracing at some point?
A great thing to do with the evening tutors.
Goal: Get Schemy with what we were trying to do. That is, suppose
that we have to write tally-value on our own, where would we
start?
Sam: We should start with documentation!
Things that would be useful within tv:
car will be useful to get the first
value.;;; (tv lst item) -> integer?
;;; lst : list?
;;; item : any?
;;; Compute the number of times the item appears in the list.
(define tv
(lambda (lst item)
(cond
; Option 1: Empty list
[(null? lst)
0]
; Option 2: Front matches
[(equal? item (car lst))
(+ 1 (tv (cdr lst) item))]
; Option 3: Front doesn't match
[else
(tv (cdr lst) item)])))
(test-equal? "Element occurs once, at front" (tv '(1 2 3 4 5) 1) 1)
(test-equal? "Edge case: Empty list" (tv '() 'a) 0)
(test-equal? "Edge case: Strange value"
(tv (list (circle 5 'solid "black")
(circle 5 'solid "red")
(circle 5 'solid "black")
(circle 5 'outline "red"))
(circle 5 'outline "black"))
0)
(test-equal? "Value appears multiple times"
(tv (list 'a 'b 'a 'a 'c 'e) 'a)
3)
Have fun. Be good to each other.