It'll be clear from the code and explaination below.
Code in stateful computation model:
declare
proc {Swap X Y}
T = {NewCell @{X}}
in
{X}:=@{Y}
{Y}:=@T
end
local A I in
A={MakeTuple array 10}
for J in 1..10 do A.J={NewCell 0} end
I = {NewCell 1}
(A.1):=2
(A.2):=1
{Swap fun {$} I end fun {$} A.(@I) end}
{Browse @I}
{Browse @(A.1)}
{Browse @(A.2)}
end
Explaination:
STEP1:
T = {NewCell @{X}} , here @{X} returns @I i.e. 1
STEP2:
{X}:=@{Y} , here {X} returns I and {Y} returns A.(@I) i.e. A.1
so this step effectively does I:=2
STEP3:
{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