Please send questions to
st10@humboldt.edu .
; in-class examples
; CS 131 - Week 2, Lecture 1
(require 2htdp/universe)
(require 2htdp/image)
; introducing a new type today: the scene type
; * it is a special kind of image
; * the image and universe teachpacks support
; this type (it has operations for it)
; how can I get a scene instance?
; I can use the operation:
; empty-scene: number number -> scene
; purpose: expects a width and a height in pixels
; and produces an empty scene with those dimensions
(empty-scene 300 200)
; remember how, last Friday, we gave an identifier a
; name using the define operation?
; we will OFTEN do that, in programs;
; These are often called NAMED CONSTANTS,
; and a common style is to write named constants in
; all uppercase;
; STARTING WITH WEEK 2 LAB/Homework 2,
; YOU should use all-uppercase for your named constants,
; too; [that's a COURSE STYLE STANDARD]
; it is VERY common to use named constants for the width and
; height of a scene:
(define WIDTH 400)
(define HEIGHT 200)
(empty-scene WIDTH HEIGHT)
(define WIDGET-MAX 13)
; place-image: image number number scene -> scene
; purpose: expects an image, the desired x and y coordinates
; (in that order, and in pixels),
; and a scene, and produces a new scene with that image
; centered at (x,y) within the given scene (and trimmmed
; to fit in the scene)
(define BACKDROP (empty-scene WIDTH HEIGHT))
(place-image (circle 30 "solid" "blue")
50
50
BACKDROP)
; do you want several images in a scene?
; well, the result of place-image is a scene;
; so, have a scene created by a place-image expression as
; the last expression in a place-image expression!
(place-image
(rectangle 20 60 "solid" "green")
50
50
(place-image (circle 30 "solid" "blue")
50
50
BACKDROP))
;*******************
; bonus after-class examples:
; ...and the above is a scene, so it could itself be the
; 4th expression to ANOTHER place-image call...
; (and so on, and so on...)
(place-image
(star 50 "solid" "purple")
100
100
(place-image
(rectangle 20 60 "solid" "green")
50
50
(place-image (circle 30 "solid" "blue")
50
50
BACKDROP))) ; see? 3 parentheses here...
; but as a class member mentioned, you could give names to
; these scenes to simplify this...
(define CIRCLE-SCENE
(place-image (circle 30 "solid" "blue")
50
50
BACKDROP))
(define STRIPED-BALL-SCENE
(place-image
(rectangle 20 60 "solid" "green")
50
50
CIRCLE-SCENE))
(define STRIPED-BALL-STAR-SCENE
(place-image
(star 50 "solid" "purple")
100
100
STRIPED-BALL-SCENE))
STRIPED-BALL-STAR-SCENE
; end of bonus after-class examples
;******************
(place-image
(rectangle 20 60 "solid" "green")
50
50
BACKDROP)
(place-image (circle 30 "solid" "red")
0 HEIGHT
(empty-scene WIDTH HEIGHT))
; OUR FIRST FUNCTION... (user-defined function)
; consider the math function f(x) = 3x
; here's what it looks like in Racket
; (and we'll discuss function syntax further on Thursday)
; defining a function -- this says, define a function with
; the name f and a single parameter variable named x,
; which will produce is 3 times that parameter's value
; when it is called/used
(define (f x)
(* 3 x)
)
; these are function applications -- applying, or using,
; or calling, that function;
(f 3)
(f 100)
(f (+ 5 5))