How to Design Programs: An Introduction to Computing and Programming [Go to first, previous, next page; contents; index]Section 35Assignment to VariablesA set!-expression, also known as an ASSIGNMENT, has the following shape: (set! var exp)It consists of a variable, the LEFT-HANDSIDE, and an expression, called RIGHT-HANDSIDE. The left-hand side of a set!-expression is a fixed variable. In this book, we only use variables that are defined, either at the top-level 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 varto (define var V)The EFFECTof this second step is that from this point on, all references to varin an evaluation replace varby V.68Its 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 WorkConsider 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/curriculum-Z-H-44.html (1 of 13) [2/5/2008 4:56:51 PM]
This preview has intentionally blurred sections.
Sign up to view the full version.