Elements

Ontology asks, What exists?,
to which the answer is Everything.
— W.V.O. Quine, Word and Object

Comments

Line comment

q)/Oh what a lovely day
Trailing comment
q)2+2  /I know this one
4
Multiline comment
/
    Oh what a beautiful morning
    Oh what a wonderful day
\
a:42
\
ignore this and what follows
the restroom at the end of the universe

Nouns

noun examples
atom 42; "a"; 1b; 2012.08.04; `ibm
vector (43;44;45); "abc"; 0101b; `ibm`goo; 2012.09.15 2012.07.05
list (43;"44";45); ("abc";0101b;);(("abc";1 2 3); `ibm`abc)
dictionary `a`b`c!42 43 44; `name`age!(`john`carol`ted;42 43)
table ([]name:`john`carol`ted; age:42 43 44)
keyed table ([name:`john`carol`ted] age:42 43 44)

Lists are zero or more items, separated by semicolons, and enclosed in parentheses. An item can be any noun.

A vector is a list in which all items are of the same datatype. It can be represented without parentheses. Numeric, boolean, char and symbol vectors have their own forms.

type example
numeric 42 43 44
date 2012.09.15 2012.07.05
char "abc"
boolean 0101b
symbol `ibm`att`ora

The syntactic class of nouns includes all data structures. Operators, functions and adverbs can be given noun syntax by listing or parenthesising them.

q)count(+)
1
q)count(+;within;\)
3

Attributes

Attributes are metadata that apply to lists of special form. They are often used on a dictionary domain or a table column to reduce storage requirements or speed retrieval.

Names and namespaces

Names consist of upper- and lower-case alphabetics. They may contain, but not begin with, underscores and numbers. For example: a, foo, foo2_bar.

What in the world is a namespace?
— Kenneth E. Iverson

A namespace is a container or context within which a name resolves to a unique value. Namespaces are children of the root namespace (usually just root) and are designated by a dot prefix. Names in the root have no prefix. The root namespace of a q session is parent to multiple namespaces, e.g. h, Q and z. (Namespaces with 1-character names – of either case – are reserved for use by Kx.)

q).z.p                         / GMT timestamp
2017.02.01D14:58:38.579614000

Names in context

Functions

Functions are:

  1. operators and primitive functions, eg +, count
  2. as defined in the lambda notation, eg {x+2*y}
  3. as derived from (1) and (2) by adverbs, eg +/, count'
  4. q-SQL functions, e.g. select

Functions are first-class objects and can be passed as arguments to other functions. Functions that take other functions as arguments are known as higher-order functions.

Operators

Operators are primitive binary functions that may be applied infix.

q)|[2;3]                 / maximum, prefix form
3
q)2|3                    / maximum, infix form
3
q)rotate[2;0 1 2 3 4 5]  / prefix form
2 3 4 5 0 1
q)2 rotate 0 1 2 3 4 5   / infix form
2 3 4 5 0 1
Operators are denoted by glyphs or reserved words or both. They cannot be defined using the lambda notation.

glyph operator glyph operator glyph operator glyph operator
= equal <> not equal ~ match
< less than <= less than or equal > greater than >= greater than or equal
+ plus - minus * times % divided by
& minimum | maximum
# take , join ^ fill; coalesce _ drop; cut
! dict; key; enum; update; delete

Minimum and maximum

The minimum operator is denoted by both the & glyph and the reserved word and. The maximum operator is denoted by both the | glyph and the reserved word or.

Reserved words

The following reserved words denote operators.

Aand, asof
Bbin, binr
Ccor, cov, cross, cut
Ddiv, dsave
Eeach, ema, except
Ffby
Iij, in, insert, inter
Llike, lj, ljf, lsq
Mmavg, mcount, mdev, mmax, mmin, mmu, mod, msum
Oor, over
Ppeach, pj, prior
Sscan, scov, set, setenvss, sublist, sv consolidate
Uuj, union, upsert
Vvs encode, vs split
Wwavg, within, wsum
Xxasc, xbar, xcol, xcols, xdesc, xexp, xgroup, xkey, xlog, xprev, xrank

Adverbs

Adverbs are primitive higher-order functions: they return derivatives (derived functions). They are denoted by six overloaded glyphs: ', /, \, ':, /: and \:.

q)+/[2 3 4]  / reduce 2 3 4 with +
9
q)*/[2 3 4]  / reduce 2 3 4 with *
24
adverb syntax

Control words

The control words do, if and while govern evaluation.

System commands

Expressions beginning with \ are system commands or multiline comments (see above).

q)/ load the script in file my_app.q
q)\l my_app.q

Scripts

A script is a text file; its lines a list of expressions and/or system commands, to be executed in sequence. By convention, script files have the extension q.

Within a script

  • function definitions may extend over multiple lines
  • an empty comment begins a multiline comment.