Docsity

Exercise Solutions: By-Value-Result Parameters and Summation with Jensen's Device, Assignments of Computer Science

Toggle sidebar
bg1
Chapter 18 Sample Solutions
Exercise 1 There are two cases to consider:
1. The lvalue of the by-result parameter is computed before the
called method executes. In this case f's by-result actual
parameter A[i] has its lvalue computed when i is 0; when g
returns, the final value of the corresponding formal parameter j
will be written to the address of A[0]. When g begins
executing, its i is aliased to f's i, and its j is treated like an
uninitialized local variable. So the assignment j = 2 sets that
local variable to 2; then the assignment i = 1 sets g's i and j's
i to 1; then the assignment j = 3 sets that local variable to 3.
When g returns, the final value of j (which is 3) is written to the
lvalue of the corresponding actual parameter. This sets A[0] to
3—even though i is no longer 0.
2. The lvalue of the by-result parameter is computed after the
called method returns In this case f's by-result actual parameter
A[i] does not have its lvalue computed before the call. When
g begins executing, its i is aliased to f's i, and its j is treated
like an uninitialized local variable. So the assignment j = 2
sets that local variable to 2; then the assignment i = 1 sets g's
i and j's i to 1; then the assignment j = 3 sets that local
variable to 3. When g returns, the lvalue of the actual parameter
corresponding to j (which is A[i]) is computed. At this time i
is 1, so the final value of j (which is 3) is written to A[1].
Exercise 2 In this code the same variable i of f is passed as twice a value-result
parameter to g. In g there are two formal parameters, a and b, which are assigned
different values. So the final value of i in f will depend on the order in which a and b
are written back. If a is written back first, the final value of i will be 2; if b is written
back first, the final value of i will be 1.
void f() {
int i = 0;
g(i,i);
}
void g(by-value-result int a, by-value-result int b) {
a = 1;
b = 2;
}
pf3

Partial preview of the text

Download Exercise Solutions: By-Value-Result Parameters and Summation with Jensen's Device and more Assignments Computer Science in PDF only on Docsity!

Chapter 18 Sample Solutions

Exercise 1 There are two cases to consider:

  1. The lvalue of the by-result parameter is computed before the called method executes. In this case f's by-result actual parameter A[i] has its lvalue computed when i is 0; when g returns, the final value of the corresponding formal parameter j will be written to the address of A[0]. When g begins executing, its i is aliased to f's i, and its j is treated like an uninitialized local variable. So the assignment j = 2 sets that local variable to 2; then the assignment i = 1 sets g's i and j's i to 1; then the assignment j = 3 sets that local variable to 3. When g returns, the final value of j (which is 3) is written to the lvalue of the corresponding actual parameter. This sets A[0] to 3—even though i is no longer 0.
  2. The lvalue of the by-result parameter is computed after the called method returns In this case f's by-result actual parameter A[i] does not have its lvalue computed before the call. When g begins executing, its i is aliased to f's i, and its j is treated like an uninitialized local variable. So the assignment j = 2 sets that local variable to 2; then the assignment i = 1 sets g's i and j's i to 1; then the assignment j = 3 sets that local variable to 3. When g returns, the lvalue of the actual parameter corresponding to j (which is A[i]) is computed. At this time i is 1, so the final value of j (which is 3) is written to A[1]. Exercise 2 In this code the same variable i of f is passed as twice a value-result parameter to g. In g there are two formal parameters, a and b, which are assigned different values. So the final value of i in f will depend on the order in which a and b are written back. If a is written back first, the final value of i will be 2; if b is written back first, the final value of i will be 1. void f() { int i = 0; g(i,i); } void g(by-value-result int a, by-value-result int b) { a = 1; b = 2; }

Exercise 3 When the following f function is evaluated, a divide-by-zero runtime exception occurs. This shows that the actual parameter 1 div 0 passed to g is evaluated, even though f never uses the corresponding formal parameter. By-name parameter passing does not evaluate an actual parameter if the corresponding formal parameter is unused, so that cannot be how ML passes parameters. fun g(x,y) = y; fun f() = g(1 div 0, 0); Exercise 4 In the following code, the function testcalls(p,i) takes a list of integers p and an integer i, and makes i recursive calls passing p. Each call accesses the head of p (to try to make sure the compiler doesn't optimize the parameter away), and the calls are not tail calls (to try to make sure the compiler actually produces function calls with parameter passing), and the parameter is changed on each call (to try to make sure the compiler does not treat the list as a loop invariant). So if list parameters are copied on calls, testcalls(L,n) for fixed n should take time proportional to the length of L. In fact, by experimenting with testcalls we see that it takes time that is independent of the length of L: whether with val L = repeatedlist(1,1) or with val L = repeatedlist(1,100000), testcalls(L,2000000) takes the same amount of time. (This experiment is good evidence that our ML does not copy lists when passed as parameters. But no such experiment can be perfect when the compiler is treated as a black box. It is just possible, though unlikely, that the compiler is smart enough to notice that testcalls(L,n) = (hd L)*n, and so optimizes the entire execution away!) fun repeatedlist(,0) = [] | repeatedlist(e,i) = e::repeatedlist(e,i-1); fun testcalls(,0) = 0 | testcalls(p,i) = (hd p)+testcalls((hd p)::p,i-1); Exercise 6 a. A[0] = 0 and A[1] = 2 b. A[0] = 3 and A[1] = 2 c. A[0] = 3 and A[1] = 2, or A[0] = 1 and A[1] = 2, depending on the order in which the value-result parameters are written back after the call d. A[0] = 1 and A[1] = 3