Where

Input: Where(f(x), Def(x, a))
$$f(x)\; \text{ where } x = a$$

Defines the symbol x as an alias for a and evaluates the expression f(x) with this bound value of x. This is equivalent to f(a).

Input: Where(a**2 + 2*a + 1, Def(a, x + y))
$${a}^{2} + 2 a + 1\; \text{ where } a = x + y$$

Using a bound variable in an expression.

Input: Where(f(a) + f(b), Def(f(x), x**2))
$$f(a) + f(b)\; \text{ where } f(x) = {x}^{2}$$

Defines the symbol f as a function and uses it in an expression. The symbol x is a dummy variable which becomes bound locally only within the Def-expression.

Input: Where(x*y*z, Def(x, 1), Def(y, x+2), Def(z, x+y+3))
$$x y z\; \text{ where } x = 1,\;y = x + 2,\;z = x + y + 3$$

There can be multiple definitions in one Where-expression. The definitions are parsed left to right: x can be used in the right-hand side defining y, both x and y can be used in the right-hand side defining z, and all three (x, y and z) can be used in the main expression.

Input: Where(a+b, Def(Tuple(a, b), T))
$$a + b\; \text{ where } \left(a, b\right) = T$$

Destructuring assignment.

Input: Where(a*d-b*c, Def(Matrix2x2(a, b, c, d), M))
$$a d - b c\; \text{ where } \begin{pmatrix} a & b \\ c & d \end{pmatrix} = M$$

Destructuring assignment. This sets a, b, c and d to the entries of the matrix M.

Input: Where(Sum(a_(i), For(i, 1, n)), Def(Tuple(a_(i), For(i, 1, n)), T))
$$\sum_{i=1}^{n} a_{i}\; \text{ where } \left(a_{1}, a_{2}, \ldots, a_{n}\right) = T$$

Special destructuring assignment. This sets n to the length of the tuple T and sets a_ to a function mapping $\{1, 2, \ldots, n\}$ to the elements of T.

Input: Where(f(2,3), Def(f(x,y), x*y + y + x**2))
$$f\!\left(2, 3\right)\; \text{ where } f\!\left(x, y\right) = x y + y + {x}^{2}$$

Defines f as a function of two variables and evaluates it. The symbols x and y are dummy variables which become bound locally only within the Def-expression.

Last updated: 2020-03-06 00:22:16