I am reading Harper Intro for SML and am a bit confused in control cells. On page 114, he gives the following example:
val r = ref 0 val s = ref 0 val _ = r := 3 val x = !s + !r val t = r val _ = t := 5 val y = !s + !r val z = !t !r
"After completing these bindings, x is tied to 3, y is tied to 5, and z is tied to 10.
Here is my trace of his code:
val r = ref 0 //allocates storage for r and sets to 0 val s = ref 0 //allocates storage for s and sets to 0 val _ = r := 3 //sets r = 3 val x = !s + !r //sets x = 0 + 3 = 3 val t = r //sets t = 3 val _ = t := 5 //sets t = 5 val y = !s + !r //sets y = 0 + 3 = 3 val z = !t !r //sets z = 5 + 3 = 8
My x is correct (3), but my y and z are both wrong (my y is 3 instead of 5, and my z is 5 instead of 10).
Where am I wrong here?
Also, why is val _ = t := 5 necessary instead of the simple t := 5 ?
Thanks bclayman
source share