Please send questions to
st10@humboldt.edu .
(require 2htdp/universe)
(require 2htdp/image)
; example from clicker question -- POOR STYLE, not using design recipe!
(define (clicker1 a-number)
(cond
[(< a-number 5) "bim"]
[(< a-number 10) "bam"]
[(< a-number 20) "bum"]
[else "boom"]
)
)
(clicker1 7)
;---------------
; remember absolute value, from Math?
; abs-value(x) = x if x >= 0
; -x if x < 0
; signature: abs-value: number -> number
; purpose: expects a number, and produces
; its absolute value:
; abs-value(x) = x if x >= 0
; -x if x < 0
(define (abs-value a-number)
(cond
[(>= a-number 0) a-number]
[else (* -1 a-number)]
)
)
(check-expect (abs-value -5) 5)
(check-expect (abs-value 27) 27)
(check-expect (abs-value 0) 0)
(abs-value -5)
; aside: sometimes cases DON'T need a cond... part 1
; modulo function:
; signature: integer integer -> integer
; purpose: expects two numbers, and produces the
; remainder from integer division of those 2 numbers
(modulo 7 2)
; modulo can be used to guarantee a result in a
; desired range ...
; FOR EXAMPLE,
; (modulo something WIDTH) will ALWAYS be in [0, WIDTH)
; (modulo something HEIGHT) will ALWAYS be in [0, HEIGHT)
; aside #2 - sometimes boolean functions -- simply
; a function that produces a boolean -- can be
; useful in variety of settings
; a boolean function is often a useful auxiliary
; function -- a function called by another
; function;
; for example, in another function's cond expression
; here's a little one, to start:
; let's write a function that, given a temperature,
; will produce whether it is in the safe range
; of [55, 75]
; signature: in-safe-range: number -> boolean
; purpose: expects a temperature in Fahrenheit,
; and produces whether it is in the safe range
; [55, 75]
(define (in-safe-range temp)
(and (>= temp 55)
(<= temp 75))
)
(check-expect (in-safe-range 40) false)
(check-expect (in-safe-range 55) true)
(check-expect (in-safe-range 65) true)
(check-expect (in-safe-range 75) true)
(check-expect (in-safe-range 76) false)
(in-safe-range 65)
(in-safe-range 76)
; refactor: is when you improve your code after
; you have initially written and debugged it
;----------------
; Scoville scale: a rating a pepper hotness
; I'd like an operation that expects a Scoville rating,
; and produces the youngest kind of person I
; should feed it to...
; my simple scale:
; (-inf, 500) -> "infant"
; [500, 1000) -> "toddler"
; [1000, inf) -> "adult"
; signature: spiciness: number -> string
; purpose: expects a pepper's Scoville rating,
; and produces the youngest kind of person I
; should feed it to, based on:
; my simple scale:
; (-inf, 500) -> "infant"
; [500, 1000) -> "toddler"
; [1000, inf) -> "adult"
(define (spiciness sco-rating)
(cond
[(< sco-rating 500) "infant"]
[(< sco-rating 1000) "toddler"]
[else "adult"]
)
)
; need at LEAST 5 tests
(check-expect (spiciness 25) "infant")
(check-expect (spiciness 500) "toddler")
(check-expect (spiciness 750) "toddler")
(check-expect (spiciness 1000) "adult")
(check-expect (spiciness 1001) "adult")
(spiciness 25)
(spiciness 750)
(spiciness 1001)