Please send questions to
st10@humboldt.edu .
; Sharon Tuttle
; CS 131 Week 1 Lab - 10:00 am
(require 2htdp/universe)
(require 2htdp/image)
; a simple expression of type number
15
; a simple expression of type string
"Hello, DrRacket!"
; a simple expression of type boolean
true
; a simple expression of type image
.
.
.
; compound expressions must have this syntax:
; (operation expression expression ...)
; for example: the arithmetic operations + - * /
; here is a compound expression using an arithmetic
; operator:
(+ 4 5 7 8)
(* (+ 4 5 7 8) 10)
; not all operations expect number expressions,
; and not all compound expressions produce numbers...
; string-append expects string expressions,
; and produces a new string that is all those strings
; smushed together
(string-append "hel" "lo")
(string-append "1" "2" "3" "4" "george" "harold")
; string-length expects a string expressions,
; and produces a number -- the number of characters
; in that string
(string-length "jello") ; notice: the type of THIS compound
; expression is number!
(+ (string-length "jello") 10)
; string=? expects string expressions and produces
; a boolean,
; whether or not those are equivalent strings
(string=? "hi" "lo")
(string=? "hi" "hi")
(string=? "Hi" "hi")
(string=? "hi" "ih")
(string=? (string-append "choc" "o" "late")
"chocolate")
; string->number expects a string with number-type contents,
; and it produces EITHER the number equivalent of that
; string, OR boolean false if it cannot do so
(string->number "42")
(string->number "13.33333")
(string->number "george")
; number->string expects a number and produces a string
; version of that number
(number->string 42)
(number->string 42.3)
; relational operations: < > <= >= =
; NOTE: that these operations produce a boolean
; NOTE: this = ONLY works for number expressions!
(< 3 5)
(>= 4 100)
; boolean operations: and or not
; and: expects boolean expressions, and produces whether
; ALL of them are true
; or: expects boolean expressions, and produces whether
; AT LEAST ONE of them are true
; not: expects ONE boolean expression, and produces true
; if it is false, and vice versa
(and true true true)
(and (< 1 3) (< 5 6))
(and (< 1 3) (> 5 6))
(or (< 1 3) (> 5 6))
; a SIGNATURE gives the name of an operation,
; the TYPES of expressions it expects, and
; the type it produces;
; circle: number string string -> image
; a PURPOSE STATEMENT DESCRIBES the expressions
; expected by an operation, and DESCRIBES what it
; produces
; purpose: it expects a radius, either "solid" or
; "outline", and a color expressed as a string,
; and produces a circle image of that size, style,
; and color
; the radius needs to be in pixels
; pixel: picture element
; ...it is one dot on the screen...
(circle 30 "solid" "purple")
(circle 0.5 "solid" "black")
; an identifier is a name a programmer chooses
; in Racket, it is almost any collection of characters
; (that AREN'T BLANKS OR WHITE SPACE)
; that aren't a number, boolean, string, and
; don't contain: ( ) [] { } " , ' ; # | \
; handy style tip: start your identifier with a letter
; and -- you can give an identifier a value with the
; define operation -- once you have done this,
; that name is a simple expression whose value is
; that value
; (define produces no value -- it has a SIDE-EFFECT,
; it gives that name a value when it is then used!)
; (define identifier expression)
(define purply (circle 30 "solid" "purple"))
purply
purply
purply
; beside: image image ... -> image
; purpose: expects images, and produces a new image
; with all those beside each other
(beside purply purply)
3
; above: image image ... -> image
; purpose: expects images, and produces a new image
; with 1 above the other etc.
(above purply purply)
; overlay: image image ... -> image
; purpose: expects images, and produces a new image
; with the first atop the second atop the third ...
; lined up on their centers
; check-expect is an odd operation;
; it expects any two expression,
; and IF each check-expect's pairs of expressions
; have the same value, a single message is printed
; saying how many tests passed;
; else, it opens up a window showing the "failed"
; tests (those check-expects where the expressions
; weren't the same)
(check-expect (+ 1 1) 2)
; note: this is an example of a test that fails...
; ...but you'll still see the overlaid circles afterwards
; (the test failing doesn't halt the execution)
(check-expect (+ 1 1) 3)
(+ 3 5)
(overlay (circle 10 "solid" "green")
(circle 20 "solid" "red")
purply)