cancel
Showing results for 
Search instead for 
Did you mean: 

Have I developed the functional equivalent of a Boolean gate?

PTECH127
New Contributor II

My input values for binary data are all returning 0!

q shell.PNG

1 ACCEPTED SOLUTION

SJT
Contributor III
Contributor III

You’re infested with parens! They might be confusing you.

First, those multiple right parens in the prompts: that’s not q trying to look like Python. Each right paren after the initial q) indicates a suspended evaluation with associated state. Unless you’re 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 it’s 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

 

View solution in original post

1 REPLY 1

SJT
Contributor III
Contributor III

You’re infested with parens! They might be confusing you.

First, those multiple right parens in the prompts: that’s not q trying to look like Python. Each right paren after the initial q) indicates a suspended evaluation with associated state. Unless you’re 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 it’s 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