I understand that the find (?) operator is rank-sensitive, although I do not see any definition of list rank in the Borror book. I believe it is the level of rectangularity of a list. This would make sense as it is analogous to array rank in APL. Unfortunately saying it is rank sensitive does not provide much additional information other than find (?) modifies it’s behavior depending on whether its operands are simple or general
There are several combinations of right and left operands (being either simple or general) when find (?) is invoked. The first atom (or first “leaf” in some cases) seems to have a bearing on the result (one case returns an unexpected 'type error). I have read pg. 406 in the Borror book and I find that the rules stated there are helpful, but incomplete. I cannot find the rules stated in any other q documentation. I have written out a 1-page outline of what I see to be the rules from several days of experimentation, but I’m not sure they are complete either at this point.
Has anyone compiled a definitive set of rules or can you provide a link to somewhere where they are listed. These rules affect the behavior of other operators as well so understanding them is critical in understanding the language.
Thanks in advance for any information.
Just an FYI the rules presented on the following Wiki page regarding the find (?) operator are incomplete:
http://code.kx.com/wiki/Reference/QuestionSymbol
OK I have spent the last 3 days working on this empirically and feel that I can explain every case except for:
q)1 2 3 ? (1 2; (1; 2 3))
'type
Despite what has been documented that ? is rank sensitive, ? supports right and left operands with mixed rank on a per item basis. But this 'type error I still cannot explain.
It seems to me the result should be:
(0 1; (0; 1 2))
I’m hoping someone here can explain why the 'type error is correct.
The issue is in following line:
q) 1 2 3 ? (1;2 3)
`type
I am not sure of the reason but here is my guess:
Rank of left operand is 1, so according to wiki it will search for object with rank 0.
Now first element of right list has rank 0. And as Charles mentioned that ‘?’ is rank sensitive and rank is defined by first element recursively. So maybe it gives an error because it finds next element to be a list and not an atom (rank 0)
.
So overall behind the scene,its behavior is based on following 2 parameters:
i) type of left operand
ii)type of first element of right operand
Thanks Rahul.
My take is a simple list can only be searched for atoms. The way the operands are set up there is an attempt to search the simple list left operand for the sub-list 2 3 and thus the 'type error. I don’t believe that statement in the Wiki is correct.
Consider the following expression which does not return the 'type error as provenance:
q)(1;2 2; 3) ? (1; 2 3)
0 3
As I mentioned that its behavior depends on the first element of right operand. For ex:
This doesn’t work:
(1 2 3) ? (1;2 3) / error : `type
But this works:
(1 2 3)?(2 3;1) / (1 2;0)
Same goes for your other example:
q)(1;2 2; 3) ? (1; 2 3)
0 3