next up previous
Next: Program Composition Up: Programs Previous: Programs

Variable Memory

After a variable is assigned, ccli can remember its previous value. For a variable is x, the expression 'x (read prev x) refers to its previous value. Note that ''x is the same as 'x. That is, ccli only remembers one step back. This is useful in programs where you might need to know the difference between the current value of a variable and its value the last time through. For example, here is one way to write the Fibonacci sequence:

include standard.ccl

program main() := {
  x := 1;
  true : {
    print ( { 'x, x }, "\n" ),
    x := x + 'x
  };
};
Executing fib(1) produces
{ 1, 1 }
{ 1, 2 }
{ 2, 3 }
{ 3, 5 }
{ 5, 8 }
{ 8, 13 }
{ 13, 21 }
{ 21, 34 }
...
ad infinitum.

Warning: Assignments such as a[i] := x or r.a = x are designed to be as efficient as possible and for technical reasons do not preserve the variable memory structure. Thus,

a := {};
a := { 1, 2, 3 };
a[0] := 0;
print ( 'a, ", ", a } );
results in {}, { 0, 2, 3 }. Thus, don't use the prev operator on array or record variables that have their parts individually assigned elsewhere.

You can also prev entire expressions. Thus

'(x+y) = 'x + 'y;
'(lamda x . x + y) z = (lambda x . x + 'y) z;
are both true under normal circumstances.


next up previous
Next: Program Composition Up: Programs Previous: Programs
Eric Klavins 2003-12-03