Please send questions to
st10@humboldt.edu .
; CIS 130 - Monday, January 27, 2010
; in-class projected notes
; (and some additions after class...)
; BSS 313 has a version of DrScheme
; installed;
; you can download the newest version
; onto your home computer
; (whether Windows, Mac, or Linux)
; for free from
; http://download.plt-scheme.org/
; start up DrScheme...
; IN BSS 313: you'll have to select
; your desired LANGUAGE in DrScheme:
; * in the Language menu, click
; "Choose Language..."
; * See the "Teaching Languages"
; section? Click the little
; triangle next to "How to Design
; Program" to get a list, and
; * ...click on "Beginning Student"
; * Now click the OK button, and
; * finally, click the "Run" button
; in DrScheme top-right to finish
; setting up the desired language
; Now you should see a message near the
; top of the INTERACTIONS window
; saying the Language: is now
; Beginning Student
; you'll probably have to do this
; every time you start work in BSS 313,
; because the computer is reset when
; you log off;
; at home, DrScheme will remember your
; latest language next time you start
; it up...
; we are typing this in DrScheme's
; DEFINITIONS window...
; (its TOP window)
; when you click the Run button in
; the top right of DrScheme,
; you see the values of the expressions
; from the DEFINITIONS window
; in the INTERACTIONS window, the
; lower window in DrScheme;
; what is a program?
; ---it is a list of instructions that
; you want the computer to do for
; you;
; it is a specific, ordered list
; of instructions for solving a
; problem
; BUT!! a computer isn't as "smart"
; as a person;
; ...you have to tell it EXACTLY and
; PRECISELY what to do,
; in a language it can understand;
; or it cannot do it!
; what is a programming environment?
; it is an environment, or setting,
; or a program that makes developing
; these sets of instructions easier;
; DrScheme is such an environment;
; these are also sometimes called
; Integrate Development Environments
; or IDE's
; in this DrScheme environment,
; we'll be developing lists of
; instructions in the programming
; language: Beginning Student level
; of Scheme;
; many languages, natural and computer,
; have grammar -- rules that are
; expected to be followed;
; in programming languages, that's
; called the language's SYNTAX
; if you type something NOT using that
; syntax -- the computer CANNOT
; handle it!
; ...you get an error message, or
; the program might not work;
; what a statement written using
; "legal" syntax MEANS is
; called its SEMANTICS
; a COMMENT is something for
; human readers that you'd like
; the computer to ignore;
; in Scheme, the SYNTAX for a comment
; is as follows:
; it starts with a ;
; and everything from the ; to the
; end of that line is IGNORED by
; the computer (that's a comment's
; SEMANTICS)
; an EXPRESSION is something that has
; a value in a programming language;
; starting with Scheme simple
; expressions;
; in Scheme, a simple expression
; is a single value, of some data
; type;
; a LITERAL is a kind of simple
; expression -- it just stands
; for a value, "literally"
; in a programming language, there
; are syntax rules for literals!
; integer numbers? type digits
; together
31 ; this is the number literal 31
; I can have a decimal point in a
; Scheme numeric literal:
31.7
; can I have two decimal points in a number>
; NO -- uncomment the next line, and see:
; 31.7.7 ; this has incorrect syntax!
; ...and you get an error message in the INTERACTIONS
; window; DrScheme cannot translate, cannot understand
; 31.7.7, because it isn't following Scheme's syntax
; rules;
; numbers CAN start with a -, though (for negative numbers)
-34.6
; not every literal is numeric --
; there are literals of different
; types as well;
; string literals
; anything surrounded by double-quotes
; is assumed to be a literal of
; type string
"CS 131"
"31.7.7" ; this is a string literal!
; (a literal of type string)
31.7 ; this is a number literal!
; (a literal of type
; number)
; another type is boolean
; there are only two boolean literals:
true
false
; true and false are expressions that
; are literals of type boolean
; teachpacks are collections of Scheme code
; that we can load and use as
; deired;
; we'll be using TWO to start:
; * the built-in universe.ss
; teachpack,
; * the fabric-teackpack.scm
; available from
; http://web.cs.wpi.edu/~kfisler/Courses/TeachScheme/Fabric/index.html
; to add a built-in teachpack:
; * under the Language menu, select
; "Add Teachpack..."
; * on the left, in the "Preinstalled
; Teachpacks", find "universe.ss"
; and click on it;
; * click the OK button;
; * click DrScheme's Run button in the
; top-left of DrScheme;
; ...now the INTERACTIONS window should
; show universe.ss as a current
; teachpack.
; to add fabric-teachpack.scm as a
; User-installed teachpack:
; * FIRST, (from home)
; download a local copy of
; fabric-teachpack.scm from:
; http://web.cs.wpi.edu/~kfisler/Courses/TeachScheme/Fabric/index.html
;
; (from BSS 313, you'd get it
; from the st10 folder on the T:
; drive... that's the older
; fabric-teachpack.scm for the lab's
; older version of DrScheme...)
;
; * under the Language menu, select
; "Add Teachpack..."
; * on the lower RIGHT, click the
; "Add teachpack to list..."
; button;
; * navigate to the place on your
; computer where there is a
; copy of fabric-teachpack.scm,
; select it, and click Open;
; * you should now see fabric-teachpack.scm
; listed on the right, under "User-installed
; Teachpacks". Click fabric-teachpack.scm,
; and THEN click the OK button in the lower
; right;
; * click DrScheme's Run button in the
; top-left of DrScheme;
; ...now the INTERACTIONS window should
; show fabric-teachpack.scm as a current
; teachpack, too!
; when you add the teachpack
; fabric-teachpack.scm, it includes
; some pre-defined names whose values are of type
; image:
chili
hat
tshirt
worm
; the above are examples of
; identifiers, names that the
; programmer gives a meaning, or value;
; worm, tshirt, chili, hat are names
; given values in the fabric
; teachpack;
; once given a value, such a name is
; also a simple expression
; in this case, because chili, for example,
; has been given an image as its
; value, the type of the expression:
chili
; ...is thus image
; that's nice, but we need more;
; we'd like more complex expressions,
; which we'll call COMPOUND expressions
; ...these are expressions built by
; combining expressions with
; operations on those expressions;
; once you know an operation on
; some type,
; you can apply that operation
; to ANY expression of that type,
; no matter how simple or compound
; here is the syntax for a Scheme
; Beginning Student compound
; expression:
; (operation expression ... expression)
; that is,
; an open-parenthesis,
; then an operation,
; then one or more expressions separated by blanks
; then a close-parenthesis
; each operation can perform its action on
; expressions of the TYPE(s) that it
; expects, that it "knows" how to handle;
; + is a provided operation on numbers;
; its semantics is, it adds them
; together;
; as long as it is given number expressions,
; it will try to produce the number
; that results from adding the values
; of those number expressions together;
(+ 3 5)
(+ 3 5 100000000 2.7)
; when you click Run, you'll see the values
; of these compound expressions, here
; 8
; 100000010.7
; ...in the Interactions window.
; note that you can also type expressions
; directly in the Interactions window,
; and when you click Enter/Return,
; you'll see the expression's value
; (but the Interactions window's contents
; aren't saved when you save your Definitions
; window contents...
; it is good for quick checks and tests you
; don't want to save...)
; so, the + operation expects any
; number of number expressions,
; and results in the sum of those
; expressions
; ANY number expression will do -- whether simple or
; compound!
(+ (+ 3 5) 6)
(+ (+ (+ 3 5) 6) 10000)
; other arithmetic operations include:
; division: /
(/ 3 5)
(/ 3 5 2)
; multiplication: *
(* 3 5)
; subtraction: -
(- 3 5)
(- 3 5 1)
; these operations expect number
; expressions -- they are UNHAPPY
; with expressions of other types;
; (+ "hat" true) ; this gives an
; ; error message!
; note: an argument is an expression
; given with an operation in a
; compound expression
; 3 and 5 are the arguments for + below:
(+ 3 5)
; (* 4 2) and (/ 1 5) are the arguments
; for + below
; (and 4 and 2 are the arguments for *,
; 1 and 5 are the arguments for /)
(+ (* 4 2) (/ 1 5))
; there are string operations --
; for example:
; string-append expects string
; expressions, and results in
; a string combining all of its
; given strings
(string-append "hi" "there" "you")
(string-append (string-append "a" "b")
"c")
; some operations expect one type and
; result in another --
(string=? "forest" "forest")
; string=? expects string expressions,
; and results in a boolean --
; true if all the strings are the
; same (are equivalent),
; false if they are not
(string=? "forest" "gump")
; here are some operations on booleans:
; and
; or
; these each expect boolean
; expressions, and produce a boolean
; as a result
(and true true)
(and true false)
(or true false)
; not
; not expects ONE boolean expression,
; and produces the opposite boolean
; expression!
(not true)
; note: EVERY compound expression
; begins and ends with a parenthesis;
; (and EVERY open parenthesis is
; expected to be followed by an
; operation!)
; (+ (3 5) ) ; thus, this fails --
; ; 3 is not an operation
; the universe and fabric teachpacks
; provide a number of operations on
; images;
; image-width is an operation that
; expects an image expression,
; and produces a number, the width
; of that image in pixels
; (pixel = picture element, one "dot"
; on your screen)
(image-width chili)
; image-height gives an image's height
; in pixels
(image-width chili)
; circle
; expects a radius in pixels,
; either the string "solid" or
; "outline",
; and a color given as a string,
; and produces a circle image of
; that radius, mode, and color
(circle 10 "solid" "red")
(circle 100 "outline" "red")
; rectangle expects
; a width in pixels,
; a height in pixels,
; "solid" or "outline"
; a color as a string,
; and results in a rectangle image of that
; color, mode, and size
(rectangle 30 20 "solid" "blue")
; overlay expects multiple
; images, and results in an expression
; where the second is atop the first
(overlay (rectangle 300 200 "solid"
"blue")
chili)
; notice I "lose" the chili if
; I change the order of the expressions
; (it is really there -- but completed
; covered up by the blue rectangle!)
(overlay chili
(rectangle 300 200 "solid"
"blue"))
; create-solid-fabric
; expects a color as a string,
; a width in pixels,
; and a height in pixels,
; and it produces an image of
; that color, width, and height
(create-solid-fabric "green" 10 20)
(create-solid-fabric "green" 1 1)
; add-horiz-stripe expects
; a color as a string (desired stripe
; color),
; a height in pixels for that stripe,
; an image to be striped
; and it produces a striped image
(add-horiz-stripe "pink" 10 chili)
; note: now trying to reconstruct after lecture
; (due to program crash before could save...!)
; MORAL: save FREQUENTLY! 8-)
; add-vertical-stripe expects
; a color as a string (desired stripe
; color),
; a WIDTH in pixels for that stripe,
; an image to be striped
; and it produces a vertically-striped image
(add-vertical-stripe "purple" 15 chili)
; remember: when an operation expects an expression
; of some type, it can handle ANY expression
; that is of that type, whether simple or
; compound!
(add-vertical-stripe "red"
8
(add-horiz-stripe "pink" 10 chili))
; add-print expects
; two images,
; the first of which is NO BIGGER than the second,
; and produces an image with the first placed atop the second
; as many times as it fits;
(add-print chili worm)
(add-print chili (create-solid-fabric "green" 300 200))
; this fails (and so it is commented out) -- chili is
; smaller that a 300x200 pixel fabric image...
;(add-print (create-solid-fabric "green" 300 200) chili)
; the hat and tshirt images are partially transparent --
; so they work well added atop an image of appropriate
; size
; (want the exact size? remember that image-height and
; image-width produce a given image's height and weight,
; respectively, in pixels...)
(add-print tshirt (create-solid-fabric "purple"
(image-width tshirt)
(image-height tshirt)))
; what image do you think results from this compound expression?
(add-print hat
(add-print chili
(create-solid-fabric "green"
(image-width hat)
(image-height hat))))
; ONE MORE THING:
; Be sure to save the contents of your Definitions window
; in a file ending with .ss or .scm --
; then, if you want to run it or work with it more later,
; you can use the Open... command in DrScheme's File
; menu to open it, run it, change it, whatever, later;