Please send questions to
st10@humboldt.edu .
; in-lecture examples
; CS 131 - Week 2, Lecture 2
(require 2htdp/universe)
(require 2htdp/image)
(define WIDTH 200)
(define HEIGHT 150)
(define BACKDROP
(place-image (circle 30 "solid" "gold")
(- WIDTH 50)
(/ HEIGHT 3)
(empty-scene WIDTH HEIGHT)))
BACKDROP
(define (triple a-number) ; <-- this is the function header
(* 3 a-number) ; <-- this is the function body
)
(triple 25)
(triple WIDTH)
; the basic function syntax
; (define (desired-name param-name param-name ...)
; expression
; )
; let's say I want MANY purple stars of many sizes
; SO -- I'm going to write a function to do so
; I'm going to write a signature for my function-to-be
; signature: purple-star: number -> image
; now, I'm going to write a purpose statement that
; DESCRIBES what the function expects and DESCRIBES
; what ir produces
; purpose: expects a desired enclosing pentagon side length
; in pixels, and produces a purple star of that size
;(define (purple-star size)
; ...
;)
; NOW -- write some specific examples of what our
; function should do -- write them as check-expect tests
(check-expect (purple-star 27)
(star 27 "solid" "purple"))
(check-expect (purple-star 46)
(star 46 "solid" "purple"))
; now complete the function body
(define (purple-star size)
(star size "solid" "purple")
)
(purple-star 30)
(purple-star 4)
; the universe teach-pack has a big-bang operation
; that can animate scenes...
; it needs an initial "state" or world --
; this can be a simple as a natural number,
; and that's what we'll start with
; it needs a function that takes the current state,
; and produces the state of the next clock tick
; 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)
; and, I need a function that gives me the current
; scene based on the current state
; I decide I want a scene with a purple star whose
; size is based on the current state in the middle
; of a scene
; signature: draw-star-scene: number -> scene
; purpose: expects the current state of the universe,
; and produces a scene with a purple star of that
; size in the middle of the scene
(define (draw-star-scene a-number)
(place-image
(purple-star a-number)
(/ WIDTH 2)
(/ HEIGHT 2)
BACKDROP)
)
; note -- this check-expect was written BEFORE
; draw-star-scene's body was filled in!
(check-expect (draw-star-scene 15)
(place-image
(purple-star 15)
(/ WIDTH 2)
(/ HEIGHT 2)
BACKDROP))
(check-expect (draw-star-scene 47)
(place-image
(purple-star 47)
(/ WIDTH 2)
(/ HEIGHT 2)
BACKDROP))
(draw-star-scene 15)
; in the SIMPLEST kind of call to the big-bang function,
; big-bang needs
; ...an initial world or state,
; (in the simplest case, a number will do),
;
; ... a call to on-tick with the name of a function
; that expects a state of the world and produces what ; the world should be after the next clock tick
; (here, it just needs to expect a number and produce
; a number) -- this will be called for each clock
; tick.
; * optionally, give on-tick a 2nd expression,
; how often the clock should tick (0.5 for every
; half second, 1/56 for every 1/56th of a second,
; etc.)
;
; ...a call to on-draw with the name of a function
; that expects a state of the world and produces
; the scene as it should be at that state of the world
; -- this will be called after each clock tick and
; the resulting scene displayed
;
; and, finally, the big-bang expression produces the
; final state of its world when the depicted animation
; is stopped
; PLEASE remember to stop it! It *can* overload and crash
; DrRacket...
(big-bang 0
(on-tick get-1-bigger)
(on-draw draw-star-scene))
; what would happen if both the star's x-coordinate and size
; are BOTH determined by the current state?
; signature: draw-star-scene2: number -> scene
; purpose: expects the current state of the universe,
; and produces a scene with a purple star of that
; size centered at (state, HEIGHT/2)
(define (draw-star-scene2 a-number)
(place-image
(purple-star a-number)
a-number
(/ HEIGHT 2)
BACKDROP)
)
(check-expect (draw-star-scene2 15)
(place-image
(purple-star 15)
15
(/ HEIGHT 2)
BACKDROP))
(check-expect (draw-star-scene2 47)
(place-image
(purple-star 47)
47
(/ HEIGHT 2)
BACKDROP))
(draw-star-scene2 15)
(big-bang 0
(on-tick get-1-bigger 1/56)
(on-draw draw-star-scene2))