Search code examples
kdbk

Indexing dictionary in depth, two cases


When indexing dictionary in depth I've found different results in the same (as I think) constructions:

q)d:`a`b!(1 2 3;4 5 6)
q)d[`a`b;0]
1 4
q)d[`a`b]0
1 2 3

Why is this happening? How q understands and distinguishes two different cases? Before this I was confident that, for example, calling dyadic function f[a;b] and f[a]b are the same. And now I am not sure even about this.


Solution

  • To index at depth you either need semi colons separating your arguments, or use the dot. Your second example,

    d[`a`b] 0
    

    Is taking the 2 lists from the dictionary values and then indexing to return the first. While

    d[`a`b;0]
    

    or

     d .(`a`b;0)
    

    Is taking the 2 lists, and then indexing at depth, taking the first element of each, due to the semi colon/dot

    When you call a dyadic function it is expecting two parameters, passing one inside the square brackets creates a projection, which is basically using an implicit semi colon, so

    f[a]b
    

    is the same as

    f[a;]b
    

    which is the same as

    f[a;b]
    

    The result of

    f[a]
    

    is a projection which is expecting another argument, so

    f[a] b
    

    evaluates f[a], then passes argument b to this function, with usual function application via juxtaposition

    Your dictionary indexing example does not create a projection, and hence the indexing is not expecting any more arguments, so the first indexing

    d[`a`b]
    

    is evaluated immediately to give a result, and then the second index is applied to this result. It would work the same for a monadic function

    q){5+til x}[5] 2
    7
    

    Like the top level dictionary index, the application is carried out and then the result is indexed, as only one argument was expected, with no projection involved