;===== ; CS 111 - Week 2 - Lecture 2 - 2024-09-05 ; Cleaned up after class! ; last modified: 2024-09-05 (require 2htdp/image) ;===== ; REPEATING this from the projected notes for today: ;===== ; CS 111 CLASS STYLE STANDARDS for identifier names ;===== ; * start with a letter ; * chosen to be meaningful, descriptive, and NOT misleading ; * named constants should be all-uppercase ; * function names should be all-lowercase (or at least ; start with a lowercase letter -- camelCase is OK) ; * parameter names should be all-lowercase (or at least ; start with a lowercase letter -- camelCase is OK) ;===== ; another named constant! ; * intended to represent a maximum temperature desired ; for making yogurt... (define MAX-YOG 35) ; in Fahrenheit ; using the named constant MAX-YOG MAX-YOG (number? MAX-YOG) ;===== ; and another named constant, in case I might be using ; a blue hexagon of this size, style, and color ; several times (define BLUE-HEX (regular-polygon 50 6 "solid" "blue")) ; using the named constant BLUE-HEX BLUE-HEX (overlay BLUE-HEX (circle 75 "outline" "red")) ;----- ; playing around with a named constant WIDGET-MAX (define WIDGET-MAX 13) (and (> WIDGET-MAX 10) (< WIDGET-MAX 20) (= MAX-YOG WIDGET-MAX)) ;===== ; check-expect - one of a FAMILY of TESTING ; functions provided by DrRacket! ;===== ; check-expect expects TWO arguments, ; an expression to be tested, ; and an expression whose value is what I HOPE ; the value of the first expression is ; and it returns nothing, ; BUT has several side-effects: ; * if all the check- expressions in the Definitions window passed, ; prints a message at the end of the Interactions ; windows saying how many tests passed ; * if any failed, a pop-up window gives info ; about those failures (check-expect (number? MAX-YOG) #true) (check-expect (boolean? "#true") #false) (check-expect BLUE-HEX (regular-polygon 50 6 "solid" "blue")) ; this one fails -- UNCOMMENT it to see the pop-up window ; that results ;(check-expect 0.33333 ; (/ 1 3)) (check-expect (image-height (square 10 "solid" "purple")) 10) ;===== ; class member noted CAN paste a repeating-decimal literal from the ; Interactions window into the Definitions window! ;===== ; kluging to get this to display in HTML! ; BUT you CAN: ; * in the Interactions/lower window, enter the expression ; (/ 1 3) ; * copy and PASTE the resulting expression from the Interactions/lower ; window into the Definitions/upper window ; * and it DOES have the value of one-third!;===== ; writing your own FUNCTIONS ;===== ; in MATH..... ; f(x) = 3x ; f(4) is equal to 12 ; f(10) is equal to 30 ; ; in BSL (Beginning Student Language) Racket, you can write a function using the ; syntax: ; ; (define (desired-new-funct param-name-1 param-name-2 ...) ; expression ; hopefully using param-name-1 etc. ; ) ; ; function header: ; (define (desired-new-funct param-name-1 param-name-2 ...) ; ; function body: ; expression ; hopefully using param-name-1 etc. ; ) ; ; once defined, desired-new-funct can be used in compound ; expressions! ; with as many arguments as you gave it parameters! ; and each time you use your new function in a compound expression, ; each parameter is set to the value of its argument, ; and the value of its body expression is the value ; of the resulting compound expression! (of the function call!) ;----- ; so, we could write Math's f(x) = 3x in Racket using: (define (f x) ; <- function header, defining a function named f with one parameter, x (* 3 x) ; <- function body, including the expression computing the function's value ) ; <| when it is used in a compound expression (f 4) ; use function f, make parameter x have the value of argument 4 (f 10) ; use function f, make parameter x have the value of argument 10 (f MAX-YOG) ; use function f, make parameter x have the value of argument MAX-YOG ;===== ; DESIGN RECIPE ;===== ; let's rewrite f to make it more readable, ; and demo our design recipe! <-- a methodical approach to writing a function ; ;===== ; STEP 1: write a signature comment for your new function ; * reminder: CLASS STYLE: only use *type* names for the ; expected arguments and return type for your function-to-be! ; ; Here, I decide that triple is a more-descriptive name than f! ; and it expects one argument of type number, ; and it returns a value of type number: ; signature: triple: number -> number ;===== ; STEP 2: write a purpose statement comment for your ; function ; purpose: expects any number ; and returns three times that number ; STEP 3: write the function header ; (with a default body of ... for NOWWWWW) ;(define (triple a-num) ; ... ;) ; STEP 4: write at least TWO tests for your function ; (and sometimes more) ; ; test: an example call of your function, with specific argument(s) ; you choose, AND what that example call's value SHOULD be, ; if it works -- and in Racket, you can use a check-expect ; expression whose arguments are this example call and its ; expected value for each test ; (there are several other possible check- functions for testing ; also -- we'll get to some of those later) (check-expect (triple 3) 9) (check-expect (triple 12) (* 12 3)) ; STEP 5 - replace the default body expression ; with one doing what you want that uses ; the parameter(s) ; ; (in class, I commented out the earlier version and repasted ; it here, with the actual body expression replacing the ... -- ; to EMPHASIZE that we write the tests BEFORE completing the ; function body -- ; but when you are using the design recipe, it is FINE to just ; go up and REPLACE the ... in Step 3's result) (define (triple a-num) (* a-num 3) ) ;----- ; and now that you have defined a function triple, ; you can use triple in compound expressions! (triple MAX-YOG) ;===== ; ANOTHER example of writing a function using the design recipe ;===== ; I want to be able to generate a teal star ; of a size I specify ;===== ; signature: teal-star: number -> image ; purpose: expects the desired distance between ; a star's points in pixels, and returns a solid ; teal star of that size (check-expect (teal-star 10) (star 10 "solid" "teal")) (check-expect (teal-star 50) (star 50 "solid" "teal")) (define (teal-star edge-size) (star edge-size "solid" "teal") ) (teal-star 100) (teal-star MAX-YOG)