Evaluation
The interpreter evaluates expressions, expression series, and expression lists.
The q session is a REPL: read-evaluate-print loop. Expressions in the session are sent to stdin by the Enter key.
Expressions sent to stdin are evaluated. The display form of the result is written to stdout (the console) unless assigned to a name.
q)2+2
4
q)a:2+2
q)
Anything following a leading forward slash, or a forward slash preceded by a space, is ignored.
q)/comment follows leading slash
q)2+2 /or a space and a slash
4
Expression series
- Expression series
-
Zero or more expressions or expression lists, separated by semicolons.
a:10;2+2
Expressions in a series are evaluated in order from left to right.
q) /zero expressions
q)2+2 /one expression
4
q)2+2;a:10 /two expressions
q)A:10;A:42 /leftmost first
q)A
42
Expressions can be empty. A trailing semicolon makes the last expression empty. An empty expression evaluates as the general null, which has no display form. So a trailing semicolon can be used to suppress printing the result of an expression.
q);;;2+2 /four expressions
4
q);;;2+2; /five expressions
Above, printing the result of 2+2
is suppressed when it is no longer the last expression.
Expression list
- Expression list
-
An expression series embraced by square brackets.
The result of evaluating an expression list is the result of evaluating its last expression, assigned or not.
q)+[2; [A:1234;B:4321] ]
4323
q)A,B
1234 4321
Prefix, infix and assignment syntax preclude the results of expression lists
q)til [A:1234;B:4321]
'rank
q)2 + [A:1234;B:4321]
'type
q)foo: [A:1234;B:4321]
'assign
The evaluation of an expression list can be used only within an argument list or another expression list.
q)set[`foo;[A:99*2;B:6*7]]
`foo
q)(foo;A;B)
42 198 42
q)a:10
q)$[a=10; [A:1b;99]; [A:0b;100] ]
99
q)A
1b
q)a:11
q)$[a=10; [A:1b;99]; [A:0b;100] ]
100
q)A
0b
Scripts
A script is a text file used as an alternative to stdin.
Where expressions in the q session are terminated (sent to stdin) by the Enter key, expressions in a script are terminated by a newline followed by a line that does not begin with a space.
Even a single space suffices to begin a continuation line; two are recommended.
Four is excessive.
Use discretion in writing expressions (and lambda definitions) over multiple lines: balance breviy and clarity.
When a script is loaded, its expressions are evaluated in order, from top to bottom.
A script line consisting only of a backslash begins a multi-line comment. A multi-line comment can be terminated by a line consisting only of a forward slash.
\
comments in a script
can straddle several lines
before ending
/
foo:{x+y*z}