[previous] [up] [next]     [index]
Next: Assignments and Functions Up: Assignment to Variables Previous: Simple Assignments at Work

Sequencing Expression Evaluations

The hand-evaluation shows that the local definition for z serves to evaluate a set!-expression and ``to throw away'' its value. After all, a set!'s true purpose is to change a definition and not to generate a value. Because this situation is quite common, Scheme also provides the begin-expression:

(begin exp-1
       ... 
       exp-n 
       exp) 
A begin-expression consists of the keyword begin followed by a sequence of n+1 expressions. The evaluation determines the values of all expressions, in order, and then throws away the first n. The value of the last expression is the value of the entire begin-expression. In general, the first n subexpressions in a begin-expression change some definitions; only the last one has an interesting value.

We can now rewrite our first sample program with set! into a short expression:

(define x 3)

(begin (set! x (+ x 2)) x)

The use of begin not only simplifies the program, it also suggests a straight-line ordering of the evaluation.

The hand-evaluation also shows that the evaluation of set!-expression introduces additional timing constraints. More concretely, the above evaluation consists of two parts: the one before and the one after the assignment exerted its effect on the state of the definitions. Before we introduced assignments, we could replace a variable by its value or a function application by the function's body whenever we wished. Now, we must wait until we truly need the value of a variable before we perform the substitution. After all, definitions may change.

While some partial ordering is always a part of computation, the timing constraints of set! are new. By altering a definition, an assignment ``destroys'' the current value. Unless the programmer carefully plans the arrangement of assignments, such an action may be fatal. The exercises illustrate the problem in more detail.


external


Exercises

Exercise 35.2.1

Evaluate the following program by hand:

(define x 1)
(define y 1)

(begin (set! x (+ x 1)) (set! y (- y 1)) (* x y))

How many time periods can we distinguish in this hand-evaluation?

Compare this with the evaluation of

(define a 5)

(* (+ a 1) (- a 1)))

Does the nesting imply an ordering among our calculations? Does the order of addition and subtraction matter? Solution

Exercise 35.2.2

Evaluate the following program by hand:

(define x 3)
(define y 5)

(begin (set! x y) (set! y x) (list x y))

How many time periods can we distinguish in this hand-evaluation?

Now evaluate the following:

(define x 3)
(define y 5)

(local ((define z x)) (begin (set! x y) (set! y z) (list x y)))

Is it true that the definition of x contains the initial value of y and y contains the initial value of x after the two set!-expressions are evaluated, no matter what the initial values are?

Discuss what the two examples teach us about time and ``destruction of values'' in definitions. Solution

Exercise 35.2.3

Evaluate the following program by hand:

(define x 3)
(define y 5)

(begin (set! x y) (set! y (+ y 2)) (set! x 3) (list x y))

How many time intervals must we distinguish in this hand-evaluation? Solution


[previous] [up] [next]     [index]
Next: Assignments and Functions Up: Assignment to Variables Previous: Simple Assignments at Work

PLT