Topic: Expressions

Online Help


Expression blocks


An expression block encloses a list of expressions, divided by semicolons. All expressions can assign to local variables. You can use expression blocks to embed short algorithmic procedures into function definitions, inline loops or any other expressions and expression blocks. There are two types of expression blocks that differ only in the way they are rendered in the output:

$Block{expr1; expr2; expr3; ...} - multiline block of expressions;
$Inline{expr1; expr2; expr3; ...} - inline block of expressions;

As the respective names imply, $Block is rendered on multiple lines so that each expression is placed on a separate line, and for $Inline all expressions are rendered on a single line sequentially from left to right.

You can use expression blocks to create multiline functions when a single expression is not sufficient to evaluate the result. Such is the quadRoots function in the example below that calculates the roots of a quadratic equation, by given coefficients a, b, c and returns them as a vector of two elements [x1; x2].

Code Output
quadRoots(a; b; c) = _
$block{

  D = b^2 - 4*a*c;
  x_1 = (-b - sqrt(D))/(2*a);
  x_2 = (-b + sqrt(D))/(2*a);
  [x_1; x_2];
}
quadRoots(2; 3; -5)

quadRoots ( a; b; c )  = 🞀D = b2 − 4 · a · c
x1 = -b −  D2 · a
x2 = -b +  D2 · a
[x1; x2]

quadRoots  ( 2; 3; -5 )  = [-2.5 1]


When you have a $Repeat inline loop you can nest multiple expressions directly inside without enclosing them with a $block/$inline element.

All expressions inside a block or inline loop are compiled, so they are executed very fast. They are evaluated sequentially from left to right and only the last result is returned at the end. However, unlike the standard expressions you cannot see intermediate results and substituted variables. This reduces readability and verifiability of calculations. So, expression blocks should be used only where they are actually needed.