;===== ; Racket examples from CS 111 - Week 1 Lecture 2, ; cleaned up after class! ; last modified: 2025-08-28 ; this is a comment! ; (syntax: anything from a semicolon to the end of the line) ; * comments are IGNORED by the computer! ; * they are there for HUMANS to read ;===== ; data type: number ; can have digits smushed together, ; optionally with a single . decimal point ; optionally starting with a + or - ; (there are a few other possibilities, also...) ; here are some simple expressions of type number 8 14 10 19 e pi 41.113 -12 +12 ; can't have a space between a leading - or + and a digit; ; this attempted simple expression below does NOT follow ; the syntax ruls for a number; ; UNCOMMENT the line below (REMOVE the ;) and click run ; to see the error message you get! ; + 12 ; this also does NOT follow number syntax: ; 41.1.1 ;===== ; in BSL Racket, you can separate expressions using blanks ; as well as newlines ; below: eight simple expressions of data type number 1 2 3 4 5 -7 5555 .45 ;===== ; data type: string ; syntax: anything written in double quotes is considered ; a simple expression literal of type string "moo" "12" "can I say \"Hi\"?" ;===== ; data type: boolean ; consists of just two values! ; for BSL Racket: the syntax for the most-basic literal simple ; expressions of type boolean ; are #true and #false ; BUT!!!! it also accepts true and #t for #true ; (considers true and #t to ; also have the value #true) ; it also accepts false and #f for #false ; (considers false and #f to ; also have the value #false) ; so - these are six simple expressions all of data type boolean #true #false #t #f true false ;===== ; data type: image ; syntax: you can paste in a copied ; .jpg or .png or .tiff or .svg ; image into DrRacket and that is considered a simple ; expression of type image ;===== ; I searched for "dachshund small image" in a browser, ; right-clicked on and copied the desired image in the ; browser results, ; then clicked on DrRacket's Interactions window, ; and used control/command v to paste in the image! ; (Edit menu -> Paste works, also) ; simple expression of type image; NOTE: pasting in an image changes the Racket format "under the hood" ; in the .rkt file so that the image can be represented -- ; in DrRacket it looks fine, ; but if you open the .rkt file in something OTHER than DrRacket, ; the format looks decidedly strange! ;===== ; compound expressions ; ; because we want to DO things! simple expressions alone are not ; enough! ; ; in BSL Racket, here is THE syntax for compound expressions: ; ; (operation argument-expression argument-expression argument-expression ...) ; ^ these expressions are called arguments ; ; operation is followed by at least one blank/newline, ; argument expressions are separated by at least one ; blank/newline ; ; semantics: what is the value of a compound expression? ; the value returned by its operation when that operation ; is executed using those argument expressions ; ; NOTE: the data type of a compound expression is the data type ; of the value its operation *returns* ; NOTE: it depends on the particular operation ; *how many* and ; *what data types* of argument expressions it expects, ; and *what order* they must be in ;===== ; some operations work on numbers and return numbers ;===== ; + is a supported operator for addition ; ; operator + expects two or more arguments of type number ; and returns the sum of it number arguments (+ 3 4 5 6) ;===== ; more common arithmetic operators that Racket supports! ; each expects two more more argument expressions of type number, ; returns a value of type number that is the result of applying ; that operation to those argument expressions (in the order they ; appear) ; ; * multiply ; / divide ; - subtraction (- 3 4) (* 19 2) (/ 2 19) ;===== ; BY THE WAY -- we said a compound expression expects one or more ; expressions after its operation; ; ...those argument expressions can be simple expressions *OR* ; compound expressions! As long as they are of the expected data type, ; they are fine! (+ (- 3 4) (* 19 2)) ;===== ; some operations have names -- functions! ; and some operations return a value with a data type ; other than number! ; for example! ;===== ; string-append expects two or more string arguments ; and returns ONE string of those arguments' values ; all smushed together (string-append "ether" "blue") (string-append "ether" " " "blue") ; and an operation can expect arguments of one type but return ; a value of another type -- for example: ;===== ; string-length expects exactly one string argument ; and returns a number, the number of characters in ; that argument's string value (string-length "ether") ; since string-length returns a number, ; a compound expression using it can be used anywhere ; a number expression can be used! (+ (string-length "ether") 10) ;===== ; operators ; < > = <= >= ; expect two or more number arguments, ; return a boolean whose value is whether the number arguments ; meet that relationship (> 3 5) (< 3 5) (= 3 (+ 1 2)) ; and ; expects two or more boolean arguments, ; returns a boolean whose value is whether ALL of those ; have the value #true (and (< 3 5) (> 10 5)) ; or ; expects two or more boolean arguments, ; returns a boolean whose value is whether at least ONE ; of those has the value #true (or (< 3 5) (> 3 5)) ; not ; expects one boolean argument, ; and returns a boolean that is the opposite of that ; argument's value (not #true) (not (> 3 5)) ;===== ; NOTE -- = operator ONLY works with argument expressions of type number! ; * but there ARE some operations to compare expressions of other types; ; * FOR EXAMPLE: ; string=? can be used to compare string expresssions, and returns ; whether its string argument expressions have the same value (string=? "moo" (string-append "m" "oo")) ;===== ; FUN FACT: DrRacket includes additional modules, collections of ; functions and other definitions; ; you can USE them by first using the special require operation ; ; require expects the name of a module, ; and, oddly, returns nothing, ; but has the side-effect of making everything defined in ; module now usable in the rest of its Definition window/.rkt file! ; ; for example: module 2htdp/image was written to support the ; "How to Design Programs", 2nd edition text, ; and includes a nice beginning collection of image-related ; operations! ; SO, I can use them after this expression: (require 2htdp/image) ; for example: ; 2htdp/image's circle function expects a desired radius in pixels, ; the desired style expressed as a string: "solid" or "outline:, ; and a color (one option is to write a known color name as a string), ; and it returns an image depicting a circle image with that radius, ; style, and color! (circle 113 "solid" "purple") ; for example: ; the beside function expects one or more images ; and returns a single image that is a horizontal "line" ; of the image argument expressions (beside (circle 113 "solid" "purple") (circle 113 "solid" "purple")) ;===== ; You can read about more of the operations/functions in ; the module 2htdp/image in the DrRacket documentation -- ; * go to the DrRacket Help menu, ; * select "Racket Documentation" near the top, ; * enter in 2htdp/image in the ...search manuals... textfield ; in the top-left corner ; ...and select the link to the 2htdp/image module's documentation ; that results -- ; ...scroll through the index on the left-hand-side, it has links to ; descriptions of the many functions in the 2htdp/image module!