I want to understand what is happening with the below statement:
sum(til 100) where 000011110000b
That line evaluates to 22, and I can't figure out why. sum(til 100)
is 4950
. where 000011110000b
returns the list 4 5 6 7
. The kdb reference page doesn't seem to explain this use case. Why does the above line evaluate to 22?
Also, why does the below line result in error
4950 where 000011110000b
Square brackets are used for function call arguments, rather than parentheses. So the above is being interpreted as:
And you can probably see now why that would evaluate to 22, i.e. it is the sum of the 5th,6th,7th,8th values of the list
til 100
, i.e. (0...99), becausewhere
is indexing intotil 100
using the boolean list000011110000b
As you will have noticed, you can omit square brackets when calling functions; but when doing so you need to be sure it will be parsed/interpreted as intended. In general, code is evaluated right-to-left so in this case
(til 100) where 000011110000b
was evaluated first, and the result passed to thesum
function.Regarding
4950 where 000011110000b
- again if we think right to left, the interpreter is trying to pass the result ofwhere 000011110000b
to the function4590
. Although4590
isn't a named function in kdb - this is the format used for IPC, i.e. executing commands on other kdb processes over TCP. So you are telling kdb to use the OS file descriptor number 4590 (which almost certainly won't correspond to a valid TCP connection) to run the commandwhere 000011110000b
. See the "message formats" section here for more details: http://code.kx.com/q/tutorials/startingq/ipc/