Please send questions to
st10@humboldt.edu .
; Sharon Tuttle
; CS 131 - Week 1 Lab
; these lines add access to the image and universe
; teachpacks
(require 2htdp/universe)
(require 2htdp/image)
13 ; simple expression of type number
pi
e
; simple expression of type string
"hello"
; a simple expression of type boolean
true
; a simple expression of type image
.
; remember: compound expressions in Racket have
; the syntax:
; (operation expression expression ...)
; arithmetic operations on numbers include
; + - * / sin cos tan and more
; this is a compound expression of type number
(+ 6 7.8 99.45)
; there are operations on strings, too
; string-append expects string expressions
; and produces a new string that is those
; strings smushed together
(string-append "he" "llo")
(string-append "he" "llo" "there")
(string-append "h" "ello t" "here")
; string-length expect a string expression and
; produces a number, the number of characters
; in that string
(string-length "hello") ; note: the type of this expression
; is number!
(+ (string-length "hello") 10)
; string=? expects strings and produces true
; if they are equivalent (have the same contents)
; and produces false otherwise
(string=? "goofy"
(string-append "goo" "fy"))
(string=? "goofy" (string-append "gof" "oy"))
(string=? "a" "a" "a" "a")
; string->number expects a suitable string
; (one that "looks" like a number) and produces
; the equivalent number
; ...interesting quirk: if the string's contents
; can't be converted reasonably to a number,
; it produces a boolean, false
(string->number "42")
(string->number "george")
; number->string expects a number and produces
; an equivalent string
(number->string 158)
(number->string 42.3)
; relational operations: > < >= <= =
; compare number expressions and produce boolean
; (this = only works to compare numbers!)
(< 1 3)
(= (+ 1 1) 2)
;(= "four" 4) ; can't do this, "four" isn't a number
; boolean operations: and or not
; and: expects boolean expressions, and produces true
; if ALL of those expressions are true
; or: expects boolean expressions, and produces true
; if AT LEAST ONE of those expressions is true
; not: expects a boolean expression, and produces
; true of that expression is false, and vice versa
(and true true)
(and (< 1 3) (< 3 5))
(and (< 1 3) (< 100 5))
(or (< 1 3) (< 100 5))
(not true)
(not (= 1 3))
; the image and universe teachpacks provide MANY
; image and otherwise graphical operations
; this is a SIGNATURE -- gives the name of an operation and
; types of expressions it expects and type it produces
; circle: number string string -> image
; this is a PURPOSE STATEMENT -- it DESCRIBES what an
; operation expects and DESCRIBES what it produces
; purpose: expects a radius in pixels, "solid" or
; "outline", and
; a color (expressed as a string), and produces
; a circle of that size, style, and color
; pixel: picture element - 1 "dot" on the screen
(circle 0.5 "solid" "black")
(circle 20 "outline" "red")
; an IDENTIFIER is a name determined by a programmer
; in Racket: just about any string of characters
; (no blanks!) that doesn't include
; ( ) [ ] { } " , ' ` # | \
; and doesn't look like a number, string, or a boolean
; nice style: start 'em with a letter...
; you can give an identifier a desired value with the
; define operation
; (define identifier expression)
; produces nothing, but has the SIDE-EFFECT that that
; identifier now has the value of that expression
; (it is now a SIMPLE EXPRESSION whose value is that value)
(define red-dot (circle 10 "solid" "red"))
red-dot
; beside: image image ... -> image
; purpose: expects images, and produces an image
; with all those images beside each other
(beside red-dot red-dot red-dot)
; above: image image ... -> image
; purpose: expects images, and produces an image
; with all those images one above each other
(above red-dot red-dot red-dot)
; overlay: image image ... -> image
; purpose: expects images and lays the first image on
; the second image on the third image ... lined up
; by their centers
(overlay red-dot
(circle 20 "solid" "blue")
(circle 40 "solid" "green"))
; one more operation: check-expect
; expects 2 expressions, and has an odd side-effect:
; IF, for all check-expects in the Definitions window,
; the two expressions produce the same value,
; you get a message saying how many tests have passed.
; ELSE, you get a separate window pointing out which
; ones didn't!
(check-expect (+ 1 1) 2)
; note: this is an example of a test that fails...
; ...but you'll still see the red dot afterwards
; (the test failing doesn't halt the execution)
(check-expect true false)
red-dot