;===== ; CS 111 - Week 2 - Lecture 1 - 2024-09-03 ; Cleaned up after class! ; last modified: 2024-09-03 ;----- ; make available the functions and definitions in the module 2htdp/image (require 2htdp/image) ;===== ; more examples of simple expressions 1234 #false "1224 #false" ;===== ; reminder: compound expression syntax: ; ; (operation expr-arg expr-arg ...) ;----- ; more examples of compound expressions (string-append "m" "oo") (+ (* 15 2.3) (/ 1 3)) (regular-polygon 40 (+ 8 1) "solid" "gray") ;===== ; two of the kinds of documentation we will be using this ; semester: ; * function signature ; * function purpose statement ; ; (our CLASS STYLE for these is STRICTER than that in ; the course text...!) ; ; a function signature is a comment that includes: ; * function's name ; * type of each of its expected arguments ; * type of the value it returns ; ...essentially, syntax info for this function! ; ; class style: ; ; signature: funct-name: type1 type2 ... -> ret-type ;----- ; for example, here is a function signature comment for ; Racket's built-in function string-length: ; signature: string-length: string -> number ;===== ; the purpose statement is a comment that basically ; provides the semantics/meaning of this function, ; and the CLASS STYLE is for it to DESCRIBE what the ; the function expects, and to DESCRIBE what it ; returns (and describes any side-effects it has) ; and class style is to explicitly SAY ; expects ... and returns ... ;----- ; for example, here is a function purpose statement comment ; for Racket's built-in function string-length: ; purpose: expects any string and returns the number ; of characters in that string ;----- ; typically, we write a function's signature and purpose statement ; comments smushed together: ; signature: string-length: string -> number ; purpose: expects any string and returns the number ; of characters in that string ;----- ; playing around with the function string-length (string-length "how much wood would a woodchuck chuck") (+ (string-length "how much wood would a woodchuck chuck") (string-length "if a woodchuck could chuck wood")) (string-length (string-append "how much wood would a woodchuck chuck" "if a woodchuck could chuck wood")) ;===== ; another Racket built-in function: string? ;----- ; signature: string?: Anything -> boolean ; purpose: expects any expression with a value ; and returns #true if it is of type string ; and returns #false otherwise (string? "moo") (string? (string-length "moo")) ;===== ; and, in the same vein: built-in functions boolean? and image? (boolean? #true) (boolean? 3) (image? 3) (image? (circle 20 "outline" "orange")) ;===== ; remember: a compound expression's arguments must have ; types its operation expects! ;----- ; signature: = : number number -> boolean ; purpose: expects two numbers, and returns whether ; their values are equal (= 3 3) (= 3 (+ 1 1 1)) ;----- ; signature: string=?: string string -> boolean ; purpose: expects two strings and returns whether ; their values contain the same characters in the ; same order (string=? "moo" "moo") (string=? "moo" "oom") (string=? "moo" "MOO") ;===== ; identifier: a name in a program that the programmer ; chooses (and defines) ; identifiers include function names, parameter names, ; and named constants ;===== ; named constant - a name a programmer chooses to give ; a constant, unchanging value ;===== ; syntax for a named constant in BSL Racket: ; ; (define DESIRED-NEW-NAME expression) ; ; semantics: define does not return anything! ; BUT when define is written using the above syntax, ; ; it has the SIDE-EFFECT of making DESIRED-NEW-NAME ; a simple expression whose value is the value of ; the given argument expression, ; ; and its type is the type of its given argument ; expression ;===== ; class style: for a name representing a never-changing value, ; write it in all-uppercase ;----- ; for example: (define MY-CHOICE "steak") ; MY-CHOICE is now a simple expression of type string, ; whose value is "steak" MY-CHOICE ; will be "steak" (string? MY-CHOICE) ; will be #true (string=? MY-CHOICE "steak") ; will be #true ;===== ; two more IMPORTANT functions that return boolean values: ; ; and: expects boolean arguments and returns whether ; ALL of them are true ; or: expects boolean arguments and returns whether ; AT LEAST ONE OF THEM is true ;===== ; signature: and: boolean boolean ... boolean -> boolean ; purpose: expects two or more boolean expressions, ; and returns #true if ALL of them have the value #true, ; otherwise it returns #false ;===== ; signature: or: boolean boolean ... boolean -> boolean ; purposeL expects two or more boolean expressions, ; and returns #false if at least ONE of them has the value #true, ; otherwise it returns #false ;----- ; playing around with and, or (or (string=? MY-CHOICE "fish") (string=? MY-CHOICE "chicken") (string=? MY-CHOICE "tofu")) (and (< 1 3) (< 1 50)) ;===== ; quick demos of a few more Racket built-in functions -- ; (you can find more in the handout "Some DrRacket Tidbits", ; in the course text "How to Design Programs", and in the ; DrRacket documentation) ;----- ; signature: above: image image ... image -> image ; purpose: expects two or more images, and returns ; a single image consisting of the first image ; above the second image above ... above the last image (above (circle 40 "solid" "purple") (rectangle 80 5 "solid" "black") (text "Hi!" 50 "darkgreen")) ;----- ; signature: string->number: string -> number ; purpose: expects a string that can ; be considered number-like, and returns ; the corresponding number (string->number "45") ;----- ; signature: number->string: number -> string ; purpose: expects a number, and returns ; a version of that number in the form of a string (number->string 45) (number->string 56.7) ;===== ; trying out more named constants (define PURPLE-STAR (star 75 "solid" "purple")) (beside PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR) (define STAR-LINE (beside PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR PURPLE-STAR))