Skip to content

Notation

What q notation have you not seen before?

Your introductory courses focused on semantics – what the primitives can do for you. That got you started.

Now we study their notation, syntax, and definitions to understand how to combine them. Most of it will be familiar.

Possibly not all.

Work patiently through the Review section, glad to encounter so much you already know, glad occasionally to learn something you did not. The Review exercises are designed only to help ensure you understand the language features presented, not what to use them for. (For that, see the exercises in the Explore section.)

Make a note of any unfamiliar form.

Either look it up now in the Reference or note it for particular attention when we explore it later.

Reference URLs for q primitives are predictable. Just use the lower-case name of the primitive or glyph:

https://code.kx.com/q/ref/apply
https://code.kx.com/q/ref/mmu
https://code.kx.com/q/ref/dollar

Review these examples (not definitions) of syntactic forms.

/ comment
6*7  / expression and trailing comment

/ DATA 
42 42.0 "4" `42 2042.04.02 0x42 0b ..      / atoms
0W 0w ..                                   / infinities
0N 0n 0Np 0Ng " " ..                       / nulls
::                                         / general null

/ DATA STRUCTURES
("a";`foggy;1942.04.02;in)                 / general list
(*;6;7)                                    / parse tree

/ vector literals
10 20 30 40 42                             / int 
"abcdef"                                   / char 
`ibm`msft`goog                             / symbol 
1000101b                                   / boolean 
..

(1 2 3;`black`blue;"abcd")                 / list of lists
(1 2 3 4;5 6 7 8)                          / matrix

`alice`bob`carol`ted!25 37 32 35           / dictionary

/ simple table
([]name;age;hair)                          / variables

([]                                        / named vectors
  name:`alice`bob`carol;
  age:25 37 32;
  hair:`fair`black`blonde)

([]                                        / like dictionaries
  (`name`age`hair!(`alice;25;`fair));
  (`name`age`hair!(`bob;37;`black));
  (`name`age`hair!(`carol;32;`blonde) )

/ keyed table
([name;age] hair;eye)                      / variables
([]name;age)!([]hair;eye)                  / two tables

/ indices 
list[3 0 1]
matrix[1 0;3]
dictionary[`ted`bob] 
table[2 0]                                 / simple table
table[2 0;`age`eye]                        / simple table
table[`age`eye]                            / simple table
ktbl @ ([]id;name)                         / keyed table

q)"abcdef"[1 0 3 9 1 4 4 5]                / out of range
"bad beef"
q)1 2 3 4 5 6[1 0 3 9 1 4 4 5]             / out of range
2 1 4 0N 2 5 5 6
"abcdef"[::] ~ "abcdef"[]                  / omitted index ~ ::
m:3 4#"abcdefghijkl"
m[::;1] ~ m[;1]                            / omitted index ~ ::

/ FUNCTIONS
/ primitive
flip where count til ..                    / unary keywords
::                                         / unary operator
+ - * % & : | ! . $ @ ..                   / binary operators (infix)
or in and within ss ..                     / binary keywords (infix)
ssr fby ..                                 / ternary keywords
\ / ' ':                                   / unary iterators (postfix)
/: \:                                      / binary iterators (infix)

/ query templates (qSQL)
[select|exec|update|delete]
  by ..
  from ..
  where ..

/ lambda notation
{[a;b]ab:a*b;(a*a)+(2*ab)+b*b}             / with signature
{ab:x*y;(x*x)+(2*ab)+y*y}                  / without signature

/ control structures
$[test;true;false]                         / ternary conditional
if[test;exp1;exp2;..]
do[exp1;exp2;exp3;..]
while[test;exp1;exp2;..]
:r                                         / return r
'`oops                                     / signal error
.[fn;args;exp]  @[fn;arg;exp]              / error traps

/ expression list
[exp1;exp2;..]

/ APPLY AND INDEX
/ apply a function
f1[x] f2[x;y] f3[x;y;z] ..                 / verb
2 3 in 3 4 5                               / infix 
{x+y*y}/                                   / postfix 
.[*;6 7]    (*) . 6 7                      / apply function to list of args
@[til;3]    til @ 3                        / apply function to an arg
til 3                                      / (prefix)
.[M;(x;y)]    M . (x;y)                    / index with list of args
@[L;3 0]      L @ 3 0                      / index at top level

/ index a list or dictionary
L[i]  M[i;j]  f3[i;j;k] ..                 / list; matrix; ..
T[rows]  T[rows;cols]  T[cols]             / table
L 2 0 1                                    / prefix
0 1 2 M'0 1 2                              / infix

/ empty indexes
rws:1 0 2j cls:3 1 0                       / e.g.
M@rws    M . enlist rws
M[rws;]          M[;cls]
M[rws;::]        M[::;cls]
M . (rws;::)     M . (::;cls)

/ PROJECTION
2*     %[;100]     count each

/ COMPOSITION
('[f;g])                                   / {f[g[x]]}
{x*x} 1000- sqrt @                         / unary 
{x*x} 1000- {x+y*z} ::                     / ternary

/ SCRIPTS

{[a;b]                                     / multiline lambda
  ab:a*b;                                  /   indented continuation line
  (a*a)+(2*ab)+b*b}                        /   indented continuation line

/
multiline comment
multiline comment
\

\
Nothing left
but comments

Exercises

  1. Make a list of what above is not familiar to you.

  2. Either look them up now in the Reference or keep the list with you as you continue.