Reputation: 185
I am studying for a final, and I have a practice problem here.
The question asks for the result of
val y = ref 1;
fun f x = (!y) + (x + x);
(f (y := (!y)+1; !y)) + (!y);
under the following parameter passing techniques:
It seems to me that for call by value, the answer is 8. However, I believe the answer for call by name is also 8, but I would expect it to be different. The reason I think it is 8:
Is this the correct answer, and if not, can someone please point out where I have gone wrong? Also, can someone explain to me how call by need would work in this situation also?
Many thanks.
Upvotes: 0
Views: 852
Reputation: 185
I found out how it works:
(y := (!y)+1; !y)
is the parameter passed to f.
f
then looks like:
fun f x = (!y) + ((y:= (!y)+1; !y) + (y:= (!y)+1; !y));
so this ends up being 1+2+3, and the final step + (!y)
adds 3 as this is the current value of y, giving 9.
Thanks for pointing out that I was still doing call-by-value.
Upvotes: 1