Session #8
Answers
What is the first major step of the HtDF recipe?
What should a good purpose statement do?
Why is a stub useful before the real function body is written?
With a correct stub and meaningful tests for an unfinished function, what is the usual desirable result?
What should guide the choice of test expectations?
What is the purpose of a function template in early HtDF?
Which is the better signature when a function always produces a non-negative whole number?
What does it mean that the HtDF recipe is not a waterfall process?
A function tall? has a test only for a tall image. What important problem remains?
If a test fails after the body is implemented, what should you check before changing correct-looking code?
The specification says a rating is poor when it is less than 4.0. What should happen at exactly 4.0?
Why are verbose expectations such as (* 2 4.2) sometimes useful?
What does the Racket naming convention tall? communicate?
Which test set best examines a boundary at 25 where values below 25 and values at/above 25 behave differently?
Why is changing a correct implementation just to satisfy an incorrect test dangerous?
Why is a stub written before the examples/tests in the HtDF recipe, and what should normally happen when meaningful tests are run against an unfinished stub?
Stubs are written to the tests have something to call. This makes it so you can make tests before writing out the entire body of the function.
Stubs are written to the tests have something to call. This makes it so you can make tests before writing out the entire body of the function.
The stub gives the function the correct name and parameters so the tests can run. Meaningful tests should normally run and fail because the stub does not yet implement the correct behavior; they should not produce an error.
The stub gives the function the correct name and parameters so the tests can run. Meaningful tests should normally run and fail because the stub does not yet implement the correct behavior; they should not produce an error.
Write a signature, purpose statement, and stub for a function named double that consumes a Number and produces twice that Number. The stub should produce a valid Number but should not implement the real calculation yet.
; Number -> Number
; take a number and returns double that value (number)
(define (double n) 10))
; Number -> Number ; take a number and returns double that value (number) (define (double n) 10))
; Number -> Number
; produce twice the given number
(define (double n) 0)
; Number -> Number ; produce twice the given number (define (double n) 0)
Write a Racket function named passing? that consumes a Number score and produces true when the score is at least 60 and false otherwise.
; Number -> Boolean
; Takes a test score (number) and returns whether it is at least 60
(define (passing? n) (>= n 60))
```racket ; Number -> Boolean ; Takes a test score (number) and returns whether it is at least 60 (define (passing? n) (>= n 60)) ```
(define (passing? score)
(>= score 60))
(define (passing? score) (>= score 60))
A function named cheap? should produce true when a price is strictly less than 100. Write three check-expect tests that test the boundary: one value below 100, exactly 100, and one value above 100.
(check-expect (cheap? 99) true)
(check-expect (cheap? 100) false)
(check-expect (cheap? 105) false)
```racket (check-expect (cheap? 99) true) (check-expect (cheap? 100) false) (check-expect (cheap? 105) false) ```
(check-expect (cheap? 99) true)
(check-expect (cheap? 100) false)
(check-expect (cheap? 101) false)
(check-expect (cheap? 99) true) (check-expect (cheap? 100) false) (check-expect (cheap? 101) false)
The purpose of area is to produce the area of a square whose side length is n. The function is (define (area n) (* n n)), but a test says (check-expect (area 3) 3) and fails. What should you change, and why?
The test should be
(check-expect (area 3) 9)
or
(check-expect (area 3) (* 3 3))
The test should be ```racket (check-expect (area 3) 9) ``` or ```racket (check-expect (area 3) (* 3 3)) ```
Change the expected value in the test from 3 to 9: (check-expect (area 3) 9). The function correctly follows the purpose; the test expectation is wrong.
Change the expected value in the test from 3 to 9: (check-expect (area 3) 9). The function correctly follows the purpose; the test expectation is wrong.