KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Have I developed the functional equivalent of a Boolean gate?

  • Have I developed the functional equivalent of a Boolean gate?

    Posted by ptech127 on March 17, 2022 at 12:00 am

    My input values for binary data are all returning 0!

    ptech127 replied 6 months, 3 weeks ago 2 Members · 1 Reply
  • 1 Reply
  • Laura

    Administrator
    March 18, 2022 at 12:00 am

    Youre infested with parens! They might be confusing you.

    First, those multiple right parens in the prompts: thats not q trying to look like Python. Each right paren after the initial q) indicates a suspended evaluation with associated state. Unless youre exploring that state, best to cut back the stack with a backslash lest you find names resolving in suspended state. (No sign of that here, but its good practice.)

     

    q){(x*y)-1 y*x}[0;0] 'type [1] {(x*y)-1 y*x} 
    ^ 
    
    q)) x / suspended state 0
    q))
    q)x / suspension cleared, no value of x 'x [0] x / suspension cleared, no value of x ^

     

    Second, redundant and missing parens in your lambda indicate the syntax is new to you. The most important syntactic rule is that there is no hierarchy of functions. Operators are binary functions with infix syntax. They have short left scope and long right scope. The left argument is the value immediately on the left. The right argument of an operator, or the argument of a unary function, is the result of evaluating everything to its right. Famously:

     

    q)3*4+5 27

     

    Syntactically, a republic of functions. No precedence rules to remember. Just the above. (One rule to rule them all)

    IO handles are integers. When you hopen a file or IO stream the handle for it is an integer and the integer behaves like a function. You can apply it. When you apply a file handle to an argument, the argument gets written to the file. The integers 0, 1, and 2 permanently refer respectively to the console, stdout, and stderr.

    So (x*y)-1 ((y)*x) tries to write y*x to stdout. (Oops, type error, needs to be text.)

    Understanding this, we can rewrite your tests:

    q){(x*y)-y*x}[0;0] 0 
    
    q){(x*y)-y*x}[0;1] 0
    q){(x*y)-y*x}[1;1] 0

     

    None of which will surprise you.

    To continue your exploration you might like to use Each Right Each Left /:: with a lambda to tabulate results:

     

    q)10 20 30 +/::1 2 3 4 5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 
    
    q)0 1{(x*y)-y*x}/::0 1 0 0 0 0

     

Log in to reply.