Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Functional programming 如何用函数式的、基于数组的语言(如K(或Q))来表达这个命令式函数?_Functional Programming_K_Kdb - Fatal编程技术网

Functional programming 如何用函数式的、基于数组的语言(如K(或Q))来表达这个命令式函数?

Functional programming 如何用函数式的、基于数组的语言(如K(或Q))来表达这个命令式函数?,functional-programming,k,kdb,Functional Programming,K,Kdb,如何用函数式的、基于数组的语言(如(或))来表达这个命令式函数 在草率的C++中: vector<int> x(10), y(10); // Assume these are initialized with some values. // BTW, 4 is just a const -- it's part of the algorithm and is arbitrarily chosen. vector<int> result1(x.size() - 4 +

如何用函数式的、基于数组的语言(如(或))来表达这个命令式函数

在草率的C++中:

vector<int> x(10), y(10); // Assume these are initialized with some values.

// BTW, 4 is just a const -- it's part of the algorithm and is arbitrarily chosen.

vector<int> result1(x.size() - 4 + 1); // A place to hold a resulting array.
vector<int> result2(x.size() - 4 + 1); // A place to hold another resulting array.

// Here's the code I want to express functionally.
for (int i = 0; i <= x.size() - 4; i++) {
    int best = x[i + 0] - y[i + 0];
    int bad = best;
    int worst = best;
    for(int j = 0; j < 4; j++) {
        int tmp = x[i + j] - y[i + 0];
        bad = min(bad, tmp);
        if(tmp > best) {
            best = tmp;
            worst = bad;
        }
    }
    result1[i] = best
    result2[i] = worst
}
向量x(10),y(10);//假设这些是用一些值初始化的。 //顺便说一句,4只是一个常量——它是算法的一部分,是任意选择的。 向量结果1(x.size()-4+1);//保存结果数组的位置。 向量结果2(x.size()-4+1);//保存另一个结果数组的位置。 //下面是我想在功能上表达的代码。 for(int i=0;i最佳){ 最佳=tmp; 最坏=坏; } } 结果1[i]=最佳 结果2[i]=最差 } 我最希望在和Q中看到这一点,但欢迎使用其他函数式语言。

在(一种方言)中:

将给予

[([4 1][5 2][6 3][7 4][8 5][8 6][8 7][8])``(结果1,结果2)作为输出

然后

(一种开源的K方言):

首先,设置一些示例值(使用与Clojure解决方案相同的值):

然后:

其中ab是上面的x和y变量。(K对变量x、y和z作了特例。)

要进一步打破这一点:

maxmin:{(|/x;&/x)}  / (max;min) pairs of x
get4:{4#y _ x}      / next 4 from x, starting at y
                    / with <4 remaining, will repeat; doesn't matter for min or max
/ maxmin applied to flipped results of get4(a-b) at each index 0..(length a)-1
maxmin@+get4[a-b;]'!#a

/ result
(4 5 6 7 8 8 8 8
 1 2 3 4 5 6 7 8)
maxmin:{(|/x;&/x)}/(max;min)对x
get4:{4#y_x}/从x开始的下一个4,从y开始

/通过将@silentbicycle的k直接移植到q产量

q)a:1+til 8
q)b:8#0
q){(max x;min x)}flip{4#y _ x}[a+b;]each til count a
4 5 6 7 8 8 8 8
1 2 3 4 5 6 7 8
另一种方法,稍微矢量化(imao):


这段代码试图做什么?它试图计算两件事:首先,在x的每一点上,我们想要在接下来的4个元素上的最大值(我们称之为Nx=max(x),在Px=0..3的位置。其次,在x的每一点上,我们想要在接下来的Px点上的最小值。
a:1+!8;b:8#0        / a is 1..8, b is eight 0s
{(|/x;&/x)}@+{4#y _ x}[a+b;]'!#a
maxmin:{(|/x;&/x)}  / (max;min) pairs of x
get4:{4#y _ x}      / next 4 from x, starting at y
                    / with <4 remaining, will repeat; doesn't matter for min or max
/ maxmin applied to flipped results of get4(a-b) at each index 0..(length a)-1
maxmin@+get4[a-b;]'!#a

/ result
(4 5 6 7 8 8 8 8
 1 2 3 4 5 6 7 8)
q)a:1+til 8
q)b:8#0
q){(max x;min x)}flip{4#y _ x}[a+b;]each til count a
4 5 6 7 8 8 8 8
1 2 3 4 5 6 7 8
q){(max;min)@\:flip 4#'(til count x)_\:x+y}[a;b]
4 5 6 7 8 8 8 8
1 2 3 4 5 6 7 8