Q – six months later

It has been six months since I started to earn my living by writing code in Q. I am still not a guru (a Q god as they call it in Q circles), but not a complete newbie either.

Things I like about Q

Q is terse. I will not write much about that. Other people argued better than I could that succinctness is power.

Q is readable. This may be a surprise for anybody who looks at Q code for the first time. Q syntax uses a lot of single letter characters or short combinations like /: as keywords. Its terseness is typically considered as decreasing readability.

I remember when I was a TA I taught a math class called “Personal business decisions” where one of the things we did was translating pieces of tax code into math formulae. The tax code was written in typical lawyerese that needed to be analyzed, broken into pieces, understood and then expressions like “amount in excess of the aforementioned value if any” converted to math expressions like (y-x)^+. It was amazing to see how a one third of a page of text was turning into a line or two of math. The resulting formulae were much more readable for me as I could see in a glance what was being expressed. However for most students they were equally misterious as the original in lawyerese.

My point in telling the story is that terseness may increase readability but only after an effort is made to train your eye to automatically parse the syntax. If you don’t know the syntax then longer, more descriptive expressions seem more readable. As one learns the syntax of a terse language like Q the optimum for readability quickly shifts towards the less verbose style.

Q supports well functional style of programming. In particular it supports currying. For example suppose we define two functions: one that doubles its argument and a second, three parameter one that represents a linear function


Then we can do regular Haskell style currying like

q)lin[1;2] double 3

Here lin[1;2] fixes first two variables (x,y) in lin and makes it a unary (monadic as they say in Q) function of the third variable.

Q, however, can do better than that: it can do sections. You can fix any variables in a function to get a function of the remaining variables. So lin[;1;2] represents x\mapsto x+2 and lin[1;;3] represents x\mapsto 1+3x while lin[;;3] represents (x,y)\mapsto x+3y.

The Q keyword “each” is roughly equivalent to Haskell’s “map”. Binary (dyadic) functions can be written infix, so we can write like this:

q)double each 1 2 3 4
2 4 6 8

Who can say this is not readable?

Writing an apostrophe right after a binary (dyadic) function makes it accept lists (of the same length) on both sides and compute the function component-wise. So we can do something like

q)(1 2 3) lin[;4;]' 5 6 7
q)21 26 31

This calculates the list lin[1;4;5],lin[2;4;6],lin[3;4;7]. Maybe it takes a little bit to get used to such modifiers (adverbs as they are called) but they are very handy.

Q is easy to learn. It is not as easy as Python, but definitely easier than Haskell. Of course, as with every language, true mastery requires years of experience, but if you are in environment where people are willing to share knowledge (as I’m lucky to be) you can get productive in two-three weeks.

Finally, working with Q is fun. There is an element of boredom in every job. The main source of boredom are repetitive tasks, things that you have to do over and over again. There is very little boilerplate in Q code.

What I don’t like in Q

Q is not free software. I wouldn’t use it for any at-home for-fun project, even if I could afford the license which I heard is very expensive.

Q is dynamically typed. It’s a matter of taste I guess but I like it when the type system protects me from my mistakes, like Haskell does. Run time errors are embarrassing.

Q is quirky. By quirky I mean that from behavior in one area one can not reliably extrapolate to similar area. One of the source of quirkiness is heavy overloading of keywords and built in functions that may do quite different things depending on what are types of their parameters. Take function sv for example. It can concatenate strings interspersing them with a separator. Very useful. Applied to symbol lists it may concatenate them with dots or sometimes with slashes, if the first symbol is a file handle. Applied to integers it will calculate b sv  a_1 a_2 ... a_n = \sum_{j=1}^n b^{n-j}a_j. I heard rumours (and I am not making this up) that if you set the parameters right, sv will solve sudoku for you.

Another example of quirkyness is exceptional treatment of lists of dictionaries keyed by symbols.
Q allows nonhomogeneous lists. Usually you can concatenate two homogeneous lists to form nonhomogeneous ones. For example you can concatenate a singleton float list and singleton integer list (comma roughly corresponds to Haskell’s “++”).

q)a:enlist 1.0;
q)b:enlist 1;

You can do a similar thing with lists of tables:

q)a: enlist ([] col1: 1 2 3; col2: 3 4 5)
q)b: enlist ([] col3: 6 7 8 )

Or lists of dictionaries (“!” with lists on both sides builds a dictionary with key on the left side and values on the right):

q)a:enlist (1 2 3)!3 4 5
q)b:enlist (2 3 4 5)!1 2 3 4
(1 2 3!3 4 5;2 3 4 5!1 2 3 4)

But here is the quirk: in general you can’t do the same with dictionaries keyed by symbols:

q)a:enlist (`a`b`c)!1 2 3
q)b:enlist (`d`e)!4 5

Although there is no problem if the dictionaries have the same key:

a b c
1 2 3
1 2 3

This result also explains what happens. Singleton lists of dictionaries keyed by symbols are silently converted by Q to tables with one row. And tables can be concatenated (with comma) only if they have the same columns…


Tags: ,

3 Responses to “Q – six months later”

  1. John Baker Says:

    I came across your fine blog while researching formal theorem provers. It’s a pleasant surprise to see you have encountered Q. Many years ago I knew the main designer of this language, Arthur Whitney, in Edmonton. Arthur was also the main force behind K and KDB+. I have looked at K and it’s cousins but as you remark the software is very expensive, (it certainly breaks my budget), I use J a more more affordable alternative. Cheers.

    • slawekk Says:

      I should have mentioned that although the full license for Q/KDB+ is expensive, there is also free of cost evaluation version. It is somewhat limited (32 bit only and server times out and has to be restarted after two hours), but quite sufficient to play with Q and learn it.

  2. Game of Life with Enterprise Components | Formalized Mathematics Says:

    […] comes with its own language called q (I have written about q on this blog in the past). Of course it would be trivial to write the Game of Life in q. […]

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: