Var..Do

Revision as of 02:56, 3 January 2009 by Lchrisman (talk | contribs)


Var «x» := «expr» Do «body»

Declares a local variable with identifier «x», and assigns it an initial value obtained by evaluating «expr». The identifier «x» can then be referred to within the «body» expression. The expression «body» is said to be the lexical context of «x», since outside of the lexical context, the identifier «x» is not recognized.

Var..Do is often used in a procedural syntax, where the declaration is followed by a semi-colon and the Do keyword is omitted, such as:

Var x := Sum(A,I);
Var y := Sum(B,I);
(x+y) * (x-y)

With this syntax, the lexical context for «x» extends from the expression immediately following the semi-colon to the end of the sub-expression that the Var..Do declaration is embedded in. For example, in the following expression the lexical scope of a is shown in red.

1 + ( Var a:=b^2; Var c := a/b; c^2 - a - c - 2 ) + 5

Index Declaration

The allowed indexes of a local variable can be declared using the syntax:

Var «x»[«indexList»] := «expr» Do «body»

an equivalent anachronism (considered deprecated) is

Var «x» := «expr» in each «indexList» Do «body»

When the allowed indexes are declared, Analytica will ensure that when «body» is evaluated, the value of «x» will not have any indexes not listed in «indexList». If the original value assigned to «x» has indexes beyond those found in «indexList», Analytica will automatically iterate, evaluating «body» multiple times one slice at a time.

Example

The following computes the standard deviation across only the time periods that are profitable:

Var earnings[Time] := revenue - expenses;
Index profitTimes := Subset(earnings>0);
SDeviation( earnings[Time=profitTimes], profitTimes )

Without the dimensional declaration restricting earnings to the Time index, Subset would complain that earnings has more than dimension in the event that revenue-expenses has an index in addition to Time. The dimensional declaration here allows the expression to fully array abstract if new dimensions are added to the model.

Atomic Declarations

A special case of the dimensional declaration is the declaration that a local variable must be atomic -- i.e., a single non-array value. In this case, the we simply specify a zero-length list of allowed indexes:

Var «x»[ ] := «expr» Do «body»

Then inside «body», «x» is guaranteed to be atomic.

Example

The following computes the log-factorial of a number in an array-abstractable fashion (i.e., works even if n is originally an array:

Var n[]:=n do Sum(Ln(1..n))

Note: The local variable can have the same identifier as a global variable, and the value of the global can appear within «expr» since that is outside the local identifier's lexical scope. Inside «body», the identifier always refers to the local variable. Having two local variables with the same identifier is not allowed.

Atomic..Do syntax

New to Analytica 4.2

Analytica 4.2 also recognizes the following syntax for declaring a local variable as atomic:

Atomic «x» := «expr» Do «body»

This syntax is equivalent to Var «x»[]:=«expr» Do «body» as long as the result of «expr» does not contain any Handles. It is actually equivalent to the following variation of Var..Do (also introduced in release 4.2):

[[MetaVar «x»[] := «expr» Do «body»


See Also

Comments


You are not allowed to post comments.