University of California, Berkeley
EECS Department - Computer Science Division
CS3 OaW Lecture 4
DBD Case Study, Variables (globals, lets, shadows)
Review : Difference Between Dates (DBD) Case Study
- Often data has multiple parts which don't add easily:
- Weight: Tons, (2000) Pounds, (16) Ounces
- Time: Millennia, (1000) Years, (365 1/4) Days, (24) Hours, (60) Minutes, (60) Seconds
- Length: Miles, (5280) Feet, (12) Inches
- Area: Square Miles, (5280 * 5280) Square Feet, (12 * 12) Square Inches
- Volume: Cubic Miles, (5280 * 5280 * 5280) Cubic Feet, (12 * 12 * 12) Cubic Inches
- Baseball: Seasons, (162) Games, (9) Innings, (3) Outs, (3) Strikes
- It often helps to convert the data to a simpler form (often the smallest listed) for comparison, sums or differences
- When the data has no natural "0" amount (as when comparing two times), choose the distance from some common global measure
- E.g., When calculating the difference between two dates, it makes sense to convert both dates to the number of seconds since, say, 1900.
- Meta-point: You saw how an expert would develop a program to calculate something non-trivial
Announcements
- Quest (half-way between a quiz and a test) scheduled in-class for 2008-09-22 (next week!)
- 5% of total grade (basically, a sanity-check)
- Open-book, open notes, no computer (as are all our exams)
- Covers everything we've seen up to that date
- There will be a review session the weekend before
- Reading for the exam is parts I and II of the book.
- Quest Review session : 2008-09-20 @ 2-5pm in 100 GPB
- Homework 0 due NOW!
- UC-WISE has been experiencing some errors (blue screens) -- we're looking into them!
- Other admin questions?
Common Confusions
- The return value of a
cond
clause can be the call to another procedure.
(cond ((equal? day 31) (member? month '(1 3 5 7 8 10 12)))
((...
Variables
Global and Local Variables
- We saw how to define a function, let's now see how to define a variable.
- Scope is the environment in which a variable is defined
: (define pi 3.14159)
pi
: pi
==> 3.14159
: x ;; from the square program earlier
==> *** ERROR -- Unbound variable: x
- We have just bound the value of the variable pi to the value 3.14159
- pi is now a global variable (because we defined it in the global environment)
- x was a parameter, and only had a value within square. It has limited scope, pi has larger scope.
- How is this different from a procedure which takes no arguments and returns something close to Pi?
: (define (approx-pi) (/ 22 7))
approx-pi
: approx-pi
==> #[procedure approx-pi]
: (approx-pi)
==> 3.14285714285714
- We have just bound the value of the variable approx-pi to the procedure which takes no arguments and has body (/ 22 7)
- How can we find out how far apart they are?
==> (abs (- pi (approx-pi)))
0.00126714285714291
- Let's see if I can use pi in a procedure, whether it'll be known
: (define (circle-perimeter diameter) (* pi diameter))
circle-perimeter
: (circle-perimeter 2)
==> 6.28318
- What happens when I use a global varialble before defining it? E.g.,
: (define (triangle-area height width) ;; 1/2 unknown
(* 1/2 height width))
triangle-area
> (triangle-area 2 3)
*** Error: unbound variable: 1/2
: (define 1/2 (/ 1 2))
: (triangle-area 2 3)
==> 3
- When should we use global variables? Seldom! Really, only in the following cases:
- When you want to define a constant that won't change throughout the program, like pi
- When you are doing testing and need to save some input / output
- Should we avoid global variables, for the most part?
- In theory, one could write a Scheme program using nothing but global variables, but that would be very bad style
- Yes, you should avoid using global variables as much as possible, truthfully. They are a bad habit.
- Any time you feel yourself needing to use a global variable, try using a parameter (argument) instead
- Isn't there some way of writing global variables so I don't confuse them with local variables?
- Yes indeed! We typically write them with *'s on both ends, e.g., *pi*, *e*
Let : A way to do local variables
- Parameters are one way to create variables to hold temporary information.
- They are useful to make our code easier to write and to save us from calculating the same thing multiple times.
- Once the procedure's body ends, the binding of a value to a value ends (as we saw with x above)
- There is another way to create local variables, let, which is a special form.
(let ( (variable1 value1)
(variable2 value2)
...
(variableN valueN) )
body )
- value can be any expression, function call, etc.
- Example:
: (let ((a (+ 1 1))
(b 3))
(+ a b))
==> 5
- Note the variable-value pairs are formatted like an association list, don't forget the double-parens in the beginning.
- The variables defined within a let can only be used within the body of the let
- Their scope is the body of the let
- A let expression is evaluated as follows
- The values value1 through valueN are evaluted in some undefined order (thus, no variable can depend on any other) and the results are saved.
- Then variable1 through variableN are bound to the saved results
- This means the values cannot refer to other variables defined with the let
- Example:
: (let ((a (+ 1 1))
(b a))
(+ a b))
==> *** ERROR -- Unbound variable: a
Shadowing
Summary
- Today's big ideas: global vs. local variables, their scope
- Smaller ideas: We can now write our programs with intuitive names for procedures!
In Lab this week you'll see...
- Variables and functions-as-data
- An actual quest former CS3 students had to take, and you'll take it (in an exam setting!)
In Life this week you'll see...
- Lots of interesting things happening with Lehman Brothers, Feds, BoFA, Wall Street
- Texas & south digging itself out from the floods
- Cal's football team licking its wounds for 2 weeks