University of California, Berkeley
EECS Department - Computer Science Division
CS3 Lecture 5 : Variables
Overview of today's lecture
Answers to questions
that have come up
- Do predicates HAVE to end in ? or is that just a
convention.
- Good question! It's just a convention, which means if you
write a predicate, you should end it with ?
Review
- Big ideas from last time: new data types words, sentences
and booleans
- Lots of new functions and ideas. Practice, practice, practice.
- Smaller ideas: special forms, short-circuit evaluation, branching,
lots of expressive power!
Variables (chapter 7)
Introduction
- A variable is a connection between a name
and a value. We also say it is a binding of a value
to a name.
- You may recall variables from algebra: 5 x - 10 = 0,
what is x?
- Every time a procedure is called, the variable takes on a
new value.
- Once a variable exists, we don't change the value of it.
How Little People Do Variables
: (define (square x) (* x x))
square
: (sqrt (+ (square 3) (square 4)))
5
- What is happening here in terms of the little people (or
contractor) model?
- sqrt company is called to bring in someone to do
sqrt. They bring in Alberto.
- + companyh is called to bring in someone to do +.
They bring in Betty.
- square company is called to bring in someone to
square 3. They bring in Chuck.
- square company is called again to bring in someone
to square 4. They bring in Doris.
- Important: Chuck and Doris are different people, so x
(in square) is different for them.
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 four 4)
four
: four
==> 4
: x ;; from the square program earlier
==> *** ERROR -- Unbound variable: x
- We have just bound the value of the variable four
to the value 4
- four 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, four has larger scope.
- How is this different from a procedure which takes no arguments
and returns the number 5?
: (define (five) 5)
five
: five
==> #[procedure five]
: (five)
==> 5
- We have just bound the value of the variable five
to the procedure which takes no arguments and has body
(return value) of 5
- How do we add these together and get 9?
==> (+ four (five))
9
- Let's see if I can use four in a procedure, whether
it'll be known
: (define (my-age-next-presidential-election age) (+ age four))
my-age-next-presidential-election
: (my-age-next-presidential-election 19)
==> 23
- Howabout my age next senate election?
: (define (my-age-next-senate-election age) (+ age six))
my-age-next-senate-election
: (my-age-next-senate-election 19)
==> *** ERROR IN my-age-next-senate-election -- Unbound variable: six
: (define six 6)
six
: (my-age-next-senate-election 19)
==> 25
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
Summary
- Today's big ideas: global vs. local variables, and their
scope
- Smaller ideas: We can now write our programs with intuitive
names for procedures!
Next Time
Puzzle : Light bulbs in the attic, switches
in the basement
- You have three light switches in the basement labeled A,
B and C.
- You have three light bulbs in the attic (where you're always
bumping your head) labeled 1, 2 and 3.
- You were told from the previous tenant that each one of the
switches controls exactly one of the bulbs.
- You would like to find out which switch controls which light.
(e.g., A-2, B-3, C-1)
- You clearly can figure it out by going to the attic three
times.
- If you're clever, you can figure it out by going to the attic
two times.
- Can you figure it out by going to the attic only once?
How?
Game : Nim [BerlekampConwayGuy82]
- Nim is played with several heaps of beans.
- When it's your turn to move, choose a heap and take as many
beans from it as you like: perhaps the whole heap, but at least
one bean.
- The winner is the player who takes the last bean.
- Let's say you play with four piles of beans containing 1,
3, 5 and 7 beans respectively.
- Should you go first or second? What's the ideal strategy?
References
- [BerlekampConwayGuy82] Elwyn Berlekamp, John H. Conway, Richard
K. Guy "Winning Ways for Your Mathematical Plays",
Academic Press, London, 1982.
WWW Maven: Dan
Garcia (ddgarcia@cs.berkeley.edu)
Send me feedback