k 中的可变范围传播

Variable scope propagation in k

我在 k 的早期版本中看到了内部函数的变量作用域传播。请参阅 http://www.math.bas.bg/bantchev/place/k.html

中的 eval: {[t;c]{x*t+y}/c}

但是如果我尝试在现代 k 中做同样的事情,我会得到一个错误:

KDB+ 3.6 2018.05.17 Copyright (C) 1993-2018 Kx Systems
q)\
  {[k]{x*x}k}3
9
  {[k]{x*x+k}k}3
'k
  [2]  k){x*x+k}
              ^
  )

那么为什么会出现这个错误呢?现代q中的这种变量范围传播'banned'吗?

确实是k4,kx最近实现的k不支持closures。事实上,您提到的文章确实在名为 "Changes to the Language":

的部分中提到了这一点

K4/q is a change over K3 in a number of significant ways, such as:

...

  • Nested functions in K4 and q cannot refer to surrounding function's local variables. (Often, the lack of this ability can be circumvented by making use of function projection.)

事实证明,缺乏词法范围支持的情况并非总是如此。虽然现在唯一正式记录的语言是 q,但仍然可以找到 k2 的参考手册,它是 1998 年左右的 k 实现,例如:http://www.nsl.com/k/k2/k295/kreflite.pdf。第 158 页的 "Local functions" 部分内容如下:

Local Functions

Suppose that the function g is defined within the body of another function f and uses the variable x in its definition, where x is local to f. Then x is a constant in g, not a variable, and its value is the current one when g is defined. For example, if:

  f:{b:3; g:{b}; b:4; g[]}

The value of f is the value of the local function g, which turns out to be 3, the value of b when g is defined, not the subsequent value 4.

  f[]
3

(顺便说一句,我强烈建议阅读整个文档)。

我不知道为什么放弃了对闭包的支持,但我认为这是因为性能原因,尤其是在进程间通信期间。