Section 35

How to Design Programs: An Introduction to Programming and Computing

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 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 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/curriculum-Z-H-44.html (1 of 13) [2/5/2008 4:56:51 PM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How to Design Programs: An Introduction to Computing and Programming The definition says that x stands for 3 . The local -expression introduces a definition for z . Its body is x so, in the past, the value of this local -expression would have been 3 (if anything). Now, with set! in the language, this is no longer true. To understand what happens, we must rewrite the program step by step until we have a final answer. The first step in the evaluation lifts the local definition: (define x 3) (define z (set! x (+ x 2))) x Next we must determine the value of (set! x (+ x 2)) . According to the general explanation of set! , this requires the evaluation of the right-hand side of the assignment: (define x 3) (define z (set! x 5)) x That value is 5 because the current value of x is 3 . Finally, the general explanation says that the effect of the set! expression is to change the value that the left-hand side variable represents. In our example this means that from now on, x is no longer 3 but 5 . The best way to express this change is to modify the definition of x for the next step: (define x 5) (define z (void)) x The value of set! is (void) , the invisible value. By replacing the set! -expression with the invisible value, we indicate that its evaluation is finished. At this point, it is easy to see that the result is
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Fall '07 term at WPI.

Page1 / 13

Section 35 - How to Design Programs An Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online