Search code examples
k

How to count each list length in K?


On a page https://shakti.com/tutorial/ I've found the solution, it is

#:'z / counts each list

The same solution was mentioned in https://code.kx.com/v2/learn/startingkdb/language/ by switching to k mode in q:

q) #:'(1 2;"abc")            / equivalent k expression
2 3

Why this expression #:' counts the number?

  • # counts
  • ' is an each Adverb
  • but what : means in this case? This is not an assignment, right?

Solution

  • On a page http://www.math.bas.bg/bantchev/place/k.html they mentioned that:

    : within |: is used to force the verb | to be interpreted as a monad, as by default ambiguities are resolved in favour of dyads

    Also here http://web.archive.org/web/20050504070651/http://www.kx.com/technical/documents/kreflite.pdf noted about the same:

    Note that whenever Each is applied to the monad of a primitive verb, as in !:' for Enumerate-Each, the monadic case must be made explicit by modifying the verb with colon. The dyadic case is assumed if no modifier is present.

    And that's make sense:

    / # want's to act as dyadic verb
      #' (1 2; "abc")
    #'[(1 2;"abc")]
    
    / make use of dyadic # behavior
      5 6 #' (1 2; "abc")
    (1 2 1 2 1;"abcabc")
    
    / monadic case
      #:' (1 2; "abc")
    2 3