The simplest way to create a local variable in a function is with the assignment operator :=. The expression X := Y means that the value of Y will be assigned to a newly created local variable whose name is X. Once X has been created, new values can be assigned to it with expressions like X = Z.
i1 : i = 22; |
i2 : f = () -> (i := 0; while i<9 do (<< i << " "; i=i+1); <<endl;) |
i3 : f() |
i4 : i |
In the next example, we show that the local variables in two invocations of a function don't interfere with each other. We write a function f which returns a newly created counting function. The counting function simply returns the number of times it has been called.
i5 : f = () -> (i := 0; () -> i = i+1) |
i6 : p = f() |
i7 : q = f() |
i8 : p(),p(),p(),p(),q(),p(),p(),q(),p(),p() |