CTM: Exercises of Chapter 1 (Exercise 7)

Posted by urban Sat, 02 Apr 2005 11:45:49 GMT

The problem

Explicit state. This exercise compares variables and cells. We give two code fragments. The first uses variables:


local X in
  X=23
  local X in
    X=44
  end
  {Browse X}
end

The second uses a cell:

local X in
  X={NewCell 23}
  X:=44
  {Browse @X}
end

In the first, the identifier X refers to two different variables. In the second, X refers to a cell. What does Browse display in each fragment? Explain.

My solution

In the first example 23 is displayed and in the second one 44.

The reason is that in the first example a second variable named X is created that shadows the first one. But the X whose value is 44 exists only during the inner local block. When Browse is executed the X refers to the first X again.

The second example displays of course 44 as we have only one variable X that refers to a cell whose value can be changed.

Tags , ,

Comments are disabled