;===== ; CS 111 - Week 3 Lecture 1 - 2026-09-08 ; ; In-class examples, cleaned up after class! ; ; last modified: 2026-09-08 ;----- ; will be using some functions from the 2htdp/image module: (require 2htdp/image) ;===== ; (just playing around with colors before class) (define SILVER (make-color 138 141 143)) (circle 50 "solid" SILVER) (circle 50 "solid" "gray") (define CHARTREUSE (make-color 223 255 0)) (circle 50 "solid" CHARTREUSE) (define JADE-GREEN (make-color 0 168 107)) (circle 50 "solid" JADE-GREEN) (circle 50 "solid" "green") ;===== ; a test written using check-expect passes ; if the expression being tested and the ; expected-value expression are EQUAL/have ; the same value (check-expect (sqrt 4) 2) ;----- ; BUT: decimal numbers are not always exactly represented ; in a digital computer -- they may be approximated. ; ; for example, uncomment the following to see that it fails: ; ;(check-expect (sqrt 2) ; 1.41421) ;===== ; ANOTHER testing function provided by BSL Racket: ; check-within ; ; check-within is a useful alternate testing function ; when real numbers or rational numbers or ; numbers with a fractional part are involved; ; has the same two first arguments as check-expect, ; BUT adds a third: the acceptable maximum DIFFERENCE ; between the first two arguments ;----- ; so, this test passes if the value of (sqrt 2) and 1.41421 ; are within 0.0001 of each other: (check-within (sqrt 2) 1.41421 0.0001) ;----- ; RECOMMENDATION: if your function returns a number that ; might have a fractional part, ; and its check-expect fails but the actual and expected values ; look very close to each other, ; try rewriting the check-expect as a check-within, ; using an appropriately SMALL-enough maximum difference ; that it shows your function's result really is ; close ENOUGH to the expected value! ;===== ; We have intro'd a few Racket predicates - ; functions that return a boolean value ; string? image? number? boolean? ; These happen to expect one argument of ANY ; type and returns whether it is of that ; function's interested type (boolean? 1) (number? "34") (string? "34") ;===== ; another built-in set of Racket predicates: ; return whether two values of a SPECIFIC type ; have the same value ; ; NOTE: operator = works ONLY with number arguments! ; ; string=? - to compare TWO string arguments for equality ; boolean=? - to compare TWO boolean arguments for equality ; image=? - to compare TWO image arguments for equality (string=? "moo" "Moo") (boolean=? #true true) (image=? (square 20 "solid" "brown") (rectangle 20 20 "solid" "brown")) ;===== ; a couple more "type conversion" functions built ; into Racket: ;----- ; string->number: expects a string with number-type-friendly ; characters, and returns a numeric version of that ; string, or #false if it cannot ;----- ; number->string: expects a number and returns a string ; equivalent (string->number "42") (string->number "moo") (number->string 35) (number->string 42.3) ;===== ; concept of SCOPE ; ; the scope of an identifier is the part of a ; program where it has defined meaning ;----- ; the scope of a named constant is from where it ; is defined to the end of its .rkt file ;----- ; I defined SILVER earlier in this file, so it is still ; in its scope here -- I can use it in expressions ; here SILVER ;----- ; the scope of a new function is from where it ; is defined to the end of its .rkt file ;----- ; the scope of a parameter is JUST the body ; expression of its function! ;----- ; PASTED this in from Week 2 Lecture 2 posted examples ; (INCLUDING the signature, purpose, and tests, but ; removing a few of the other comments) ;----- ; signature: cyan-star: number -> image ; purpose: expects the desired distance between ; the points of a star and returns an image ; of a cyan star of that size (check-expect (cyan-star 10) (star 10 "solid" "cyan")) (check-expect (cyan-star 4) (star 4 "solid" "cyan")) (define (cyan-star star-size) (star star-size "solid" "cyan") ) ;----- ; now that I have defined cyan-star, I can USE it here ; (and for the rest of this .rkt file) (beside (cyan-star 20) (cyan-star 30)) ;----- ; BUT the scope of cyan-star's parameter star-size ; is ONLY lines 171-172 above -- uncomment the ; following to see that it causes a "this variable ; is not defined" error: ; ; star-size ; ; (because a parameter's scope is only its function's ; body, it is considered undefined outside of that scope) ;===== ; DESIGNING more functions using the DESIGN RECIPE ;===== ; I decide I want to be able to generate ; a Humboldt logo of a specified size ;===== ; THINKING about the problem: ; * how can I represent a size? number seems like a good type ; for that; ; * how can I represent a logo? image seems like a good type ; for that; ;===== ; signature: humboldt-logo: number -> image ; purpose: expects a desired Humboldt logo size ; in pixels, and returns an 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 75) (beside (text "H" 75 "forestgreen") (text "." 75 "gold"))) (define (humboldt-logo logo-size) ; remember, originally 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 75) ;===== ; I decide I would like a silly function that ; takes an exclamation and phrase and responds ; doubtingly to it, including the exclamation in ; all-uppercase and repeating the phrase doubtingly ; (with ??) ;===== ; THINKING about the problem: ; * expects TWO pieces of information -- an exclamation ; AND a phrase; ; * an exclamation can be represented by a string; ; * a phrase can be represented by a string; ; * a function can only return ONE value, and a doubting ; response with the exclamation and repeating the phrase ; can be represented reasonably by a string ;===== ; signature: doubt-it: string string -> string ; purpose: expects an exclamation and a given phrase ; and returns a phrase with the exclamation ; uppercase with and exclamation mark ; followed by the given phrase repeated with ; two question marks (check-expect (doubt-it "oh" "the water is cold") "OH! the water is cold?? the water is cold??") (check-expect (doubt-it "wow" "the stove is hot") "WOW! the stove is hot?? the stove is hot??") (define (doubt-it exclamation phrase) ; remember, originally 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 exclamation) "! " phrase "?? " phrase "??") ) (doubt-it "wow" "the stove is hot")