;===== ; in-class examples for Week 3 Lecture 1 - 2025-09-09 ; Cleaned up after class! ; last modified: 2025-09-11 ; will be using some functions from the 2htdp/image module: (require 2htdp/image) ; CORRECTED after class Week 3 Lecture 2, 2025-09-11: ; ...the scene data type and its basic operations are provided ; by the 2htdp/image module, not 2htdp/universe, so COMMENTING OUT ; the require expression for 2htdp/universe, which we did NOT ; need for the Week 3 Lecture 1 examples! ; ;(require 2htdp/universe) ;===== ; comparing/contrasting some built-in ; predicates (functions that return a boolean) ;----- ; string? expects an argument of any type, and returns whether ; it is of type string: (string? "moo") ;----- ; string=? expects two string arguments, and returns whether ; both contain the same characters: (define COW-SOUND "moo") (string=? "moo" COW-SOUND) ;----- ; (and boolean? and boolean=? are analogous for boolean values...) (boolean? true) (boolean=? #true true) ;----- ; image? and image=? also exist: (image? (circle 10 "solid" "blue")) (image=? (circle 10 "solid" "blue") (circle (+ 5 5) "solid" "blue")) ;----- ; = is used to compare two number expressions for equality, ; but number? can be used to determine whether an expression ; is of type number: (number? (image-width (square 20 "outline" "darkblue"))) (= 5 (+ 2 3)) ;===== ; I decide I would like a way to generate ; Humboldt logos of different sizes -- ; that is, to specify a size and get a logo ; of that size ;===== ; signature: humboldt-logo: number -> image ; purpose: expects a desired Humboldt logo size ; in pixels, and returns a image of an uppercase ; forest-green H and a gold period of that size (check-expect (humboldt-logo 50) (beside (text "H" 50 "forestgreen") (text "." 50 "gold"))) (check-expect (humboldt-logo 150) (beside (text "H" 150 "forestgreen") (text "." 150 "gold"))) (define (humboldt-logo logo-size) ; remember, originially put this special template-expression ; for the function body expression: ; ... ; until AFTER writing humboldt-logo's tests/check-expect ; expressions; only then did we replace the ... with the ; body expression below! (beside (text "H" logo-size "forestgreen") (text "." logo-size "gold")) ) (humboldt-logo 136) (humboldt-logo 10) (humboldt-logo 200) ;===== ; I decide I would like a silly function that ; takes an exclamation and phrase and responds ; doubtingly to it, repeating the phrase ;===== ; signature: doubt-it: string string -> string ; purpose: expects an exclamation and a statement ; to doubt, and returns a new string that contains ; the exclamation in all-uppercase, an exclamation ; point and a blank, that statement to doubt ; followed by 2 question marks and a blank, ; followed by that statement to doubt again ; followed by 2 question marks (check-expect (doubt-it "oh" "the water is cold") "OH! the water is cold?? the water is cold??") (check-expect (doubt-it "Jeez" "Your legs are short") "JEEZ! Your legs are short?? Your legs are short??") (define (doubt-it exclam stmt-to-doubt) ; remember, originially put this special template-expression ; for the function body expression: ; ... ; until AFTER writing doubt-it's tests/check-expect ; expressions; only then did we replace the ... with the ; body expression below! (string-append (string-upcase exclam) "! " stmt-to-doubt "?? " stmt-to-doubt "??") ) (doubt-it "oh" "the water is cold") (doubt-it "Jeez" "Your legs are short") ;===== ; another built-in data type, ; (corrected after Week 3 Lecture 2, on 2025-09-11) ; provided by 2htdp/image: ; ; scene - special kind/subtype of image; ; like a background! but you can put things in it! ; transparent, and it won't let contents overlap its edges; ;===== ; (corrected after Week 3 Lecture 2, on 2025-09-11) ; 2htdp/image includes two most-basic scene operations: ;----- ; signature: empty-scene: number number -> scene ; purpose: expects the desired width and height of a scene ; in pixels, and returns a scene of that width and height (empty-scene 200 100) ;----- ; signature: place-image: image number number scene -> scene ; purpose: expects an image, the desired x and y coordinates, ; and a scene, and returns a new scene with the image ; centered at that (x, y) coordinate (and trimmed to fit ; the scene if necessary) ; (but (0, 0) is the top-left...!) ; larger x values are further RIGHT, ; larger y values are further DOWN...!!!!!) ;----- ; results in a scene of a 30-pixel-radius purple circle ; centered at 50 pixels from the left, 100 pixels down ; in a scene that's 200 pixels wide, 200 pixels high (place-image (circle 30 "solid" "purple") 50 100 (empty-scene 200 200)) ;----- ; fourth argument of place-image can be ANY expression ; whose type is scene -- and place-image returns a scene! ; SO, for example, a place-image expression can be the 4th argument ; of another place-image expression! ;---- ; so, this results in a scene with a 35-pixels-between-its-points ; pink star centered at 100 pixels from the left, ; 150 pixels down, and a 30-pixel-radius purple circle ; centered at 50 pixels from the left, 100 pixels down, ; in a scene that's 200 pixels wide, 200 pixels high (place-image (star 35 "solid" "pink") 100 150 (place-image (circle 30 "solid" "purple") 50 100 (empty-scene 200 200))) ;----- ; more with scenes on Thursday!