Home > Uncategorized > Q programming language tutorial

Q programming language tutorial

Installing:

Put following files to c:\tools\kdb: q.k, w32\q.exe

setx QHOME c:\tools\kdb

Exiting:

q)\\

or:

q)exit 0

Switching between q and K:

\

Load script:

q)\l script.q

Start as server:

q -p 5001

Connect to server and assign a connection to variable hdl:

q)hdl: hopen `:localhost:5001

Communicate from client to server (synchronous message):

q)hdl "put command here"

Closing connection:

q)hclose hdl

Atoms:

q)5 / this is comment. Before was an expression, immediatelly evaluated. Integer atom evaluates to itself
5
q)`symbol / symbol atom
`symbol

Simple arithmetic:

q)2+2
4
q)2-3
-1
q)3%4 / division
0.75

Variables:

q)x:5 / define x
q)x / get value of variable
5
q)x+x / use arithmetic with variable
10

Simple functions:

q){x*x}[4] /square function, simple expression,x is an implicit parameter, applied to atom 4.
16
q){x*x} 4 /if applied to one parameter, square brackets can be ommited
16
q)f:{[a;b]b+a*a} /function with explicit parameters
q)f [4;5] /apply to parameters. This is full syntax which must be used if params more than 1
21
q)f /will display function definition
{[a;b]b+a*a}
q)+ /built-in functions will be printed like this, it's not a symbol!
+
q)+[4;5] /can apply 2-arg functions in this way
9
q)g:{[a;b]c:a*a;c+b} /statements in function are separated by semicolon ;
q)g [4;5]
21
q)k:{7} /no-arg function, returns a constant
q)k[] /application
7
q)g[4][5] /partial application, or currying: first apply g to 4, then apply result to 5
21
q)p:{x*x} /just one-arg function
q)p 4
16
q)p@4 /@ is a two-arg 'apply' function which applies one-arg function to arg
16
q)@[p;4] /can to be called this way
16
q)`p 4 /you can address a function via symbol (only in application!)
16
q)u: `p /variable u holds symbol `p
q)u 4 /this will apply 4 to p, since u references p via symbol
16

Lists:

q)x: 1 2 3 / define a list of items of same type using simplified syntax
q)x
1 2 3
q)b: (9 9 9) /more verbose syntax
q)b
9 9 9
q)v:(9;1;8;2;7;3;6;4;5) /most verbose syntax
q)v
9 1 8 2 7 3 6 4 5
q)n:() /empty list
q)n
q)m:(7) /attempt to create a single-item list will not work, brackets are ignored and atom is evaluated
q)m
7
q)enlist 7 /create a single-list element. Displayed starting with comma to distinguish from atoms
,7
q)z: 7 6 5 4 3 2 1
q)z[0] /access by index. Result is atom. Syntax is like function application
7
q)z[5 4 1] /access by indexes. Result is list
2 3 6
q)z[x] /works this way too
6 5 4
q)z 0 /also works without brackets
7
q)z 5 4 1 /and this way too
2 3 6
q)z x /and this way too
6 5 4
q)z[] /empty indexing returns whole list
7 6 5 4 3 2 1
q)z[20] /indexing outside of list bounds returns empty value for list item type
0N
q)z[4]:9 /assignment of list item
q)z
7 6 5 4 9 2 1
q)z[4]:3
q)z
7 6 5 4 3 2 1
q)z@1 / another way of indexing, since lists are like functions
6

q)"mystring" 0 1 /strings are just list of chars
"my"
q)y:4 4 4

q)5#z /take first 5 elements, # is a two-arg function, you can use spaces around it
7 6 5 4 3
q)-4#z /take last 2 elements
4 3 2 1
q)8#x /if list is shorter than left arg, then items will repeat
1 2 3 1 2 3 1 2
q)2 _ z /drop first 2 elements, _ is a two-arg function
5 4 3 2 1
q)x+5 / add an atom to each element in list
6 7 8
q)x,y /joining lists, also works in joining list to atom
1 2 3 4 4 4
q)count 4 5 6 7 /function, returns length of list
4
q)count x
3
q)count y
3
q)#:[1 2 3 4 5] / in fact, function # is overloaded and one-arg is count. Use : to explicitly invoke one-arg
5

q)x+y /add corresponding elements of lists, works only on lists of same length
5 6 7

q)z ? 2 /search value in list from left, returns index of first found value
5
q)y ? 4 /will return 0, since it is an index of first item in list
0
q)z ? 12 /if value is not found, then returns index of element beyound last (it is equal to size of list)
7
q)z = 4 /returns a binary value with each bit in 1 if element equals to atom
00010000b
q)(x+1) = y /also works on lists of the same size, compares corresponding elements
001b
q)where z>4 /returns list of indexes which match the condition
0 1 2
q)z where z>4 /you can apply a list of indexes back to list to get values
7 6 5

q)mixed:("string";1;`mysymbol;1 2 3) /mixed list, must be enclosed in () and separated with ;
q)mixed[0] /indexing works in a same way
"string"
q)mixed[3]
1 2 3
q)mixed[0 3;2] /semicolon allows to use indexing in depth, for example: take items 0 and 3, and from each take sub-item 2
"r"
3

q)p each x /apply function p to each value in list x and return a list of results
1 4 9

Functions and Lists:

q)count 4 5 6 7 /application syntax: count is one-arg function applied to single arg of list type
4
q)count[4 5 6 7] /the same, using explicit application syntax
4
q)f 4 5 /apply single argument of type list to two-arg function, it's a partial application
{[a;b]b+a*a}[4 5]
q)f[4 5] /the same
{[a;b]b+a*a}[4 5]
q)f[4 5;1 3] /explicit syntax is used to apply two args, each is a list, both have same size
17 28
q)p @ 4 /apply function to single arg of type atom
16
q)p @ 4 5 /apply function to single arg of type list
16 25
q)f @ 4 5 /since it's like f[4 5], it's a partial application
{[a;b]b+a*a}[4 5]
q)f . 4 5 /curry-style application: function . accepts a single arg of type list, and puts elements into args
21

Dictionaries:

q)monthdays: `Jan`Feb`Mar`Apr`May ! 31 28 31 30 31 / composed from two lists of same length using !
q)monthdays
Jan| 31
Feb| 28
Mar| 31
Apr| 30
May| 31

q)monthdays[`Jan] / obtain value by key using function application syntax
31

q)key monthdays / function key returns list of keys
`Jan`Feb`Mar`Apr`May
q)value monthdays / function value returns list of values
31 28 31 30 31
q)monthdays ? 31 / search value, returns key. Searches from left to right
`Jan
q)where monthdays = 31 / list of keys for which condition of value holds
`Jan`Mar`May
q)2#monthdays / take first 2 key-value pairs as dictionary (sub-dictionary)
Jan| 31
Feb| 28

Tables:

q)dictX: `month`days!(`Jan`Feb`Mar`Apr`May;31 28 31 30 31) / this is called a column dictionary: dictionary which values are lists of same length
q)dictX
month| Jan Feb Mar Apr May
days | 31 28 31 30 31

q)tableX: flip dictX / this is a table. Table is a list of dictionaries with same keys for all list elements
q)tableX
month days
----------
Jan 31
Feb 28
Mar 31
Apr 30
May 31

q)tableY:((`month`days!(`Jan;31));(`month`days!(`Feb;28));(`month`days!(`Mar;31));(`month`days!(`Apr;30));(`month`days!(`May;31)))
q)tableY
month days
----------
Jan 31
Feb 28
Mar 31
Apr 30
May 31

q)tableX[1] / get table row by index, returns dictionary
month| `Feb
days | 28
q)tableX[1][`month] / you can first get row by index, then obtain values by column names
`Feb
q)3#tableX / take function returns table containing first 3 rows
month days
----------
Jan 31
Feb 28
Mar 31

q)select from tableX where month = `Mar
month days
----------
Mar 31
q)select days from tableX where month = `Mar
days
----
31
q)select days from tableX
days
----
31
28
31
30
31
q)`tableX insert (`Jun;30) / adding rows to table, accepts untyped list, returns list of inserted row numbers. Table must be referenced by symbol
,5
q)tableX
month days
----------
Jan 31
Feb 28
Mar 31
Apr 30
May 31
Jun 30

q)meta tableX / shows meta info about table
c | t f a
-----| -----
month| s
days | j

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: