Warning! You are being recorded (and transcribed) (provided the technology is working correctly).
Approximate overview
Academic/Scholarly
Cultural
Peer
Wellness
Misc
Lots of techniques. It’s okay that you’re struggling to figure out which.
Don’t bang your head against the wall for too long. Stop. Let your subconscious work on things. Ask questions.
Important to think about what type of thing you want. (Is this asking for a procedure, a string, a list?)
Decompose!
You’re pretty and good. You can do this.
How should we write “add 5 to all”?
(map (cut (+ 5 <>)) lst)
(map (o increment increment increment increment increment) lst)
(map + (make-list (length lst) 5) lst)
(map (lambda (x) (+ 5 x)) lst)
(define add5 (lambda (x) (+ 5 x))) (map add5 lst)
(map increment (map increment (map increment (map increment (map increment lst)))))
Could you explain how this expression works (and what it does)?
(map (o (cut (- <> (char->integer #\0)))
char->integer)
'(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
We have a procedure that converts a character to an integer and then subtracts something from that number.
It subtracts the collating sequence number for 0.
Reminder: We want to skip over code (and equations). However, the most important information is often there.
That holds for hard words, too.
How does reduce differ from apply?
reducetakes a two-parameter procedure and repeatedly applies it to neighboring pairs.
applyapplies an n-parameter procedure “all at once”.
For operations that can take two paremeters or many, like
+andstring-append, they will behave the same.
For operations that require two parameters, and return a value of the same type, reduce is essentiial.
What issues should we pay attention to in documenting substring?
;;; (substring str start end) -> string?
;;; str : string?
;;; start : integer?, a 0-based index (<= 0 start (length str))
;;; end : integer?, a 0-based index (<= 0 end (length str))
;;; Returns the substring of `str` denoted by the start index `start`
;;; (inclusive) and end index `end` (exclusive).
Our goal is that the documentation provides enough information that a careful reader can determine the results for any inputs.
For
substring, the original documentation left some of the following unclear.
Can I write
(substring "hello" 1 1)? Yes.
Add “If start equals end, returns the empty string”.
Can I write
(substring "hello 3 2)? No.
Add “(<= start end)” or something similar.
Can I write
(substring "hello" 1.0 3.0)? No.
Add
exactsomewhere in there.
start : (all-of exact-nonnegative-integer? (less-equal (length str)))
end : (all-of exact-nonnegfative-integer? (greater-equal star) (less-equal (length str)))
start : exact integer, 0 <= start <= (length str)
What about
(substring "hello" 5 5)
When do we write a test procedure?
I’m going to write a new procedure. Let’s think about some sample inputs and outputs.
We should write tests as we prepare to write any new procedure (and when writing tests does not appear to be too onerous).
What is the difference between writing a test procedure and writing a functional procedure?
I’d say it’s a “test expression” rather than a procedure. In one case, you’re writing something that checks whether something else works (e.g., by providing sample input and output). In the other, you’re writing the algorithm that converts the input to output.
We do sometimes write a procedure that we use in testing. Sometimes, that’s a less efficient but provably correct way of achieving the solution. Sometimes, that’s something that helps us decompose the testing process.
Can we look at testing bound-grade?
Sketch a set of tests for the bound-grade procedure, which takes a real
number as input and outputs.
By “sketch”, we mean “list the tests you’d write”.
TPS! Let’s see how many different tests we can come up with$a
(test-= "slightly more than 100"
(bound-grade 101)
100
0)
(test-= "something in the range"
(bound-grade 33)
33
0)
(test-= "something really large"
(bound-grade 1232135231213212112132112321)
100
0)
(test-= "inexact non integer in range"
(bound-grade 5.2)
5.2
0)
(test-= "negative numbe"
(bound-grade -2)
0
0)
(test-= "especially negative number"
(bound-grade -999999999)
0
0)
(test-= "negative inexact real"
(bound-grade -909.12321)
0
0)
(test-= "rational in range"
(bound-grade 90/4) ; Yay! Racket lets us write fractions
90/4
0)
(test-= "something slightly more than 100"
(bound-grade (+ 100 1/99999999999999999999))
100
0)
(test-= "something slightly less than 100"
(bound-grade (- 100 1/99999999999999999999))
(- 100 1/99999999999999999999)
0)
Sam’s questions:
dedup-adjacent with something other than numbers?Sam’s favorite tests:
(test-equal? (dedup-adjacent (make-list 100 "a")) (list "a"))(test-equal? (dedup-adjacent (list)) (list))Student observations:
4 equal to 4.0?Triangles
(describe-triangle 10 5 5)?