How to Design Programs: An Introduction to Computing and Programming
[Go to
first
,
previous
,
next
page;
contents
;
index
]
Section 35
Assignment to Variables
A
set!
expression, also known as an
ASSIGNMENT
, has the following shape:
(set! var exp)
It consists of a variable, the
LEFT

HAND
SIDE
, and an expression, called
RIGHT

HAND
SIDE
. The left
hand side of a
set!
expression is a fixed variable. In this book, we only use variables that are
define
d,
either at the toplevel or in a
local
expression. A
set!
expression may occur wherever an expression is
legal.
The value of a
set!
expression is always the same and is moreover invisible. It is therefore irrelevant.
What matters about a
set!
expression, instead, is the effect of its evaluation. Specifically, for the first
step of the evaluation of a
set!
expression, we determine the value of
exp
. Let's say this value is
V
. For
the second step, we change the definition of
var
to
(define var V)
The
EFFECT
of this second step is that from this point on, all references to
var
in an evaluation replace
var
by
V
.
68
Its former value is lost.
Understanding the true nature of assignments is difficult. We therefore first consider a simple though
useless example.
35.1
Simple Assignments at Work
Consider the following definition and expression:
(define x 3)
(local ((define z (set! x (+ x 2))))
x)
file:///C/Documents%20and%20Settings/Linda%20Graue...How%20to%20Design%20Programs/curriculumZH44.html (1 of 13) [2/5/2008 4:56:51 PM]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document