Approximate overview
Events
What did you learn from the traces? (foo vs. bar; baz vs. qux)
(if TEST #t #f) is a long way of
writing TEST.if; we only evaluate the first and
then use that to decide whether to use the second or third.How do we document values. E.g., if I defined a red-circle.
;;; red-circle : image?
;;; A medium-sized red circle with a black outline
(define red-circle
(overlay (circle 40 'outline 'black)
(circle 40 'solid 'red)))
Why do computer scientists use foo and bar (and baz and qux)?
There are multiple hypotheses.
Some claim it comes from the WWII military term “fubar”, “fouled up beyond all recognition”.
But others observe that “foo” predates WWII, appearing in a Daffy Duck cartoon, the Smokey Stover comic strip, and elsewhere.
In any case, these are the “metasyntactic variables”
Could we trace something else with conditionals that suggests why it’s important that we not evaluate all the parameters?
Sure.
;;; In my world, anything divided by zero is 1.
(define strange-divide
(lambda (x y)
(if (zero? y) 1 (/ x y))))
; (strange-divide 2 3)
; --> (if (zero? 3) 1 (/ 2 3))
; ; Evaluate the test
; --> (if #f 1 (/ 2 3))
; ; The test is false, so move on to the alternate (parameter 3 of if)
; --> (/ 2 3)
; ; Parentheses! Evaluate the 2, evaluate the 3, do the division
; --> 2/3
; (strange-divide 2 0)
; --> (if (zero? 0) 1 (/ 2 0))
; ; Evaluate the test
; --> (if #t 1 (/ 2 0))
; ; The test is true, so move on to the consequent (parameter 2)
; --> 1
; We're done
; Suppose we used the normal evaluation strategy
; (strange-divide 2 0)
; --> (if (zero? 0) 1 (/ 2 0))
; ; Evaluate the test
; --> (if #t 1 (/ 2 0))
; ; Evaluate the (/ 2 0)
; --> Error message
Moral:
ifhas a different evaluation strategy, which means you can have sometimes erroneous or otherwise problematic code in one side and use the condition to determine whether or not to run.
Does and do the same thing?
Yes. If the first parameter to
andis#f, it stops immediately and returns false.
How should we trace and
Evaluate the first parameter
If it’s false, replace the and with #f
If the first parameter is true, replace
(and #t ...)with(and ...).
Why does the autograder give 0.999999
It helps with grading later.
Can we start lab?
Yes
“Twenty minutes” became 45. Are you always so bad at estimating times?
Usually
Can I go to the bathroom?
You are adults, you can feel free to leave my classroom at any time. As long as you are respectful to your partners.
Some notes from Sam
median-of-3.
(Well, that’s at least one solution.)(define is-even? (lambda (x) (if (zero? (remainder x 2)) #t #f)))
is correct but also inelegant.
foo/bar.(define is-even-integer? (lambda (x) (if (integer? x) (if (zero? (remainder x 2) #t #f)) #f))) is also both correct and inelegant.
is-even? rather than repeating its body.baz/qux for simplification.median-of-3 without conditionals
(define median-of-3 (lambda (x y z) (- (+ x y z) (min x y z) (max x y z))))both-even? with only one call to is-even? (and
no calls to remainder).
(define (both-even? x y) (is-even? (some-function-of x y))).