这个 q/kdb+ 功能的名称是什么?是否有任何 LISP 实现它?如何?

What is the name of this q/kdb+ feature? Does any flavor of LISP implement it? How?

q 编程语言有一个特性(this tutorial 称为“函数投影”),其中可以使用比所需参数更少的参数调用具有两个或更多参数的函数,但结果是一个中间对象, 直到传递完所有剩余参数后,该函数才会执行;一种理解方式是函数的行为类似于多维数组,因此 (f[x])[y] 等同于 f[x;y]。例如...

q)add:{x+y}
q)add[42;]
{x+y}[42;]
q)add[42;][3]
45
q)g:add[42;]
q)g[3]
45

由于 q 没有词法作用域,这个特性在通过将必要的变量作为部分参数列表传递给内部函数来获得词法作用域行为时变得非常有用;例如可以使用此功能构造打印参数装饰器:

q)printParameterDecorator:{[f] {[f;x] -1 "Input: ",string x; f x}f};
q)f: printParameterDecorator (2+);
q)f 3
Input: 3
5

我的问题:

  1. 术语“函数投影”是一个标准术语吗?还是此功能在函数式编程文献中有不同的名称?
  2. 是否有各种 LISP 实现了此功能?哪些?
  3. 能否提供一些示例 LISP 代码?

我想另一种方法:

q)f:2+
q)g:{"result: ",string x}
q)'[g;f]3
"result: 5"

是复合函数,把3传给f,然后把f的结果传给g。 不知道是不是LISP,但是可以达到同样的效果

Is the term "function projection" a standard term? Or does this feature carry a different name in the functional programming literature?

不,你通常称它为partial application

Does any variety of LISP implement this feature? Which ones?

几乎所有的 Lisp 都允许你部分地应用一个函数,但通常你需要显式地编写一个闭包。例如在 Common Lisp 中:

(defun add (x y)
  (+ x y))

来自 alexandria 的效用函数 curry 可用于创建闭包:

USER> (alexandria:curry #'add 42)
#<CLOSURE (LAMBDA (&REST ALEXANDRIA.1.0.0::MORE) :IN CURRY) {1019FE178B}>

USER> (funcall * 3)  ;; asterisk (*) is the previous value, the closure
45

生成的闭包等效于以下闭包:

(lambda (y) (add 42 y))

像 OCaml 这样的一些函数式语言只允许函数有一个 单个 参数,但是在语法上你可以定义多个参数的函数:

(fun x y -> x + y)

以上等同于:

(function x -> (function y -> x + y))

另见 What is the difference between currying and partial application?


铌。事实上 q documentation 将其称为部分应用程序:

Notationally, projection is a partial application in which some arguments are supplied and the others are omitted