Please send questions to
st10@humboldt.edu .
; BEWARE -- in this text-version,
; there are dots (.) below where the penguin images should be!
; CS 131 - Week 3, Lecture 1
; in-class examples
(require 2htdp/image)
(require 2htdp/universe)
; SO -- conditional expression lets us
; specify branching situations...
; SYNTAX:
; (cond
; [boolean-expr1 expresion1]
; [boolean-expr2 expression2]
; ...
; [else expression-n]
; )
;
; SEMANTICS:
; * boolean-expr1 is evaluated; if it is true, the cond
; expression's value is expression1
; * OTHERWISE, boolean-expr2 is evaluated; if it is true,
; the cond expression's value is expression2
; * OTHERWISE... (continuing for each boolean expression
; ...)
; * ...and if you reach an else, the cond expression's
; value is expression-n
; MAKE SURE THIS IS CLEAR -- as SOON as a true boolean expression
; is found, its expression becomes the value of the whole
; cond expression, and that's it -- none of the other
; cond expressions is evaluated.
;-------------------------------------------------
; let's develop a function that can produce
; a greeting appropriate to the time of day
; signature: greet: string number -> string
; purpose: expects a person's name and the hour
; of the day expressed in 24-hour time,
; and it produces a greeting to that person
; appropriate to the time of day
; (morning is [0, 11],
; afternoon is (11, 17],
; evening is (17, 23])
; NOTE: this is an overly-trusting function,
; it greets one in the evening for any out-of-bounds
; time
(define (greet name time)
; remember: greet's body was only filled in AFTER the
; check-expects (yes, all 5 of them, in this case) were
; written;
(cond
[(and (>= time 0)(<= time 11))
(string-append "Good morning, " name)]
[(and (>= time 0) (<= time 17))
(string-append "Good afternoon, " name)]
[else (string-append "Good evening, " name)]
)
)
(check-expect (greet "Ann" 8)
"Good morning, Ann")
(check-expect (greet "Lonyx" 15)
"Good afternoon, Lonyx")
(check-expect (greet "Carol" 21)
"Good evening, Carol")
(check-expect (greet "David" 11)
"Good morning, David")
(check-expect (greet "Katherine" 17)
"Good afternoon, Katherine")
(greet "Ann" 8)
; the design recipe template step -- after the check-expects
; were written, before the function body was completed:
;
; for a 3-case situation, a template for the function
; body would be:
; (cond
; [... ...]
; [... ...]
; [else ...]
; )
; now for a floating penguin that lands...
; images are from
;http://wallofgame.com/free-online-games/arcade/231/Parachute_Penguin_Shootout.html
(define FLOATING-PENGUIN.)
(save-image FLOATING-PENGUIN "floating-penguin.png")
(define LANDED-PENGUIN.)
(save-image LANDED-PENGUIN "landed-penguin.png")
(define WIDTH 200)
(define HEIGHT 250)
(define BACKDROP (empty-scene WIDTH HEIGHT))
;-------------
; signature: get-1-bigger: number -> number
; purpose: expects a number, and produces the number
; that is 1 bigger
(define (get-1-bigger a-number)
(+ a-number 1)
)
; note -- this check-expect was written BEFORE get-1-bigger's
; body was filled in!
(check-expect (get-1-bigger 15) 16)
(get-1-bigger 45)
;-------------------
; signature: draw-penguin: number -> scene
; purpose: expects a number, and if that number
; is in the range
; [0, (- HEIGHT (* .5 (image-height LANDED-PENGUIN))) ),
; produces a scene with a floating penguin
; whose y-coordinate is that number;
; otherwise, produces a scene with a
; standing penguin at a y-coordinate of
; (- HEIGHT (* .5 (image-height LANDED-PENGUIN)))
(define (draw-penguin a-number)
; again, remember -- this function body was completed
; AFTER developing the check-expects
; (and after inserting a 2-case cond expression template)
(cond
[(< a-number
(- HEIGHT (* .5 (image-height LANDED-PENGUIN))))
(place-image FLOATING-PENGUIN
(/ WIDTH 2)
a-number
BACKDROP)]
[else
(place-image LANDED-PENGUIN
(/ WIDTH 2)
(- HEIGHT (* .5
(image-height LANDED-PENGUIN)))
BACKDROP)]
)
)
(check-expect (draw-penguin 5)
(place-image FLOATING-PENGUIN
(/ WIDTH 2)
5
BACKDROP))
(check-expect (draw-penguin (+ HEIGHT 1000))
(place-image LANDED-PENGUIN
(/ WIDTH 2)
(- HEIGHT (* .5
(image-height LANDED-PENGUIN)))
BACKDROP))
(check-expect (draw-penguin
(- HEIGHT (* .5
(image-height LANDED-PENGUIN)))
)
(place-image LANDED-PENGUIN
(/ WIDTH 2)
(- HEIGHT (* .5
(image-height LANDED-PENGUIN)))
BACKDROP))
; remember -- STOP this animation when you are done with it,
; even though it "looks" stopped, it is actually keeping on
; redrawing that standing penguin with each clock tick of
; the running universe...!
(big-bang 0
(on-tick get-1-bigger)
(on-draw draw-penguin))