Thursday, February 5, 2009

Ch6, Ex9

With "Call By Name", argument is evaluated everytime its needed, It doesn't work because in the code we need a[i] twice, first time i is one so we refer to a[1], second time when we refer to a[i] then i is 2 so we refer to a[2] though we intended to refer to a[1].
It'll be clear from the code and explaination below.
Code in stateful computation model:
proc {Swap X Y}
T = {NewCell @{X}}
local A I in
A={MakeTuple array 10}
for J in 1..10 do A.J={NewCell 0} end
I = {NewCell 1}
{Swap fun {$} I end fun {$} A.(@I) end}
{Browse @I}
{Browse @(A.1)}
{Browse @(A.2)}

T = {NewCell @{X}} , here @{X} returns @I i.e. 1

{X}:=@{Y} , here {X} returns I and {Y} returns A.(@I) i.e. A.1
so this step effectively does I:=2

{Y}:=@T , here {Y} returns A.(@I) i.e. A.2
so this step effectively does A.2:=2

Its clear that when, in STEP3, it was intended to change A.1, we unknowingly did it to A.2 .

No comments:

Post a Comment