对 javascript 函数返回带有许多粗箭头的多个函数感到困惑
Confused about javascript function returning multiple functions with many fat arrow
我的计算机作业有问题。
我需要访问函数的 x
值,但我的代码返回一个空函数而不是值
我用 google 搜索了所有的柯里化和闭包,但是 none 已经足够先进,可以帮助我解决我的问题
const pair = (x, y) => f => f(x, y); // Do not edit this function
const head = p => //Answer here
console.log(head(pair(1,2))) // Do not edit this
当我尝试所有组合时,我的控制台一直返回 functions
而不是
function(a,b){return a;}
您可以像这样更改 head
函数:
const pair = (x, y) => f => f(x, y);
const head = f => f(a => a)
console.log(head(pair(1,2)))
my console keeps returning me this instead
function(a,b){return a;}
让我们让它更容易阅读。在 ES5 中,您的代码如下所示:
var pair = function(x, y) {
return function(f) {
return f(x, y);
}
};
var head = function(p) {
return function(a, b) {
return a;
}
};
您需要将head
返回的函数传递给pair(1, 2)
返回的函数。所以你需要交换调用函数的顺序:
console.log(pair(1, 2)(head()));
不确定我是否正确理解你的问题,但我猜你正在尝试这样做
const pair = (x, y) => f => f(x, y);
const head = (x, y) => x;
console.log((pair(1,2)(head)))
如果不是,那么上面的这个是正确的
const pair = (x, y) => f => f(x, y);
const head = f => f(a => a);
console.log(head(pair(1,2)))
我的计算机作业有问题。
我需要访问函数的 x
值,但我的代码返回一个空函数而不是值
我用 google 搜索了所有的柯里化和闭包,但是 none 已经足够先进,可以帮助我解决我的问题
const pair = (x, y) => f => f(x, y); // Do not edit this function
const head = p => //Answer here
console.log(head(pair(1,2))) // Do not edit this
当我尝试所有组合时,我的控制台一直返回 functions
而不是
function(a,b){return a;}
您可以像这样更改 head
函数:
const pair = (x, y) => f => f(x, y);
const head = f => f(a => a)
console.log(head(pair(1,2)))
my console keeps returning me this instead
function(a,b){return a;}
让我们让它更容易阅读。在 ES5 中,您的代码如下所示:
var pair = function(x, y) {
return function(f) {
return f(x, y);
}
};
var head = function(p) {
return function(a, b) {
return a;
}
};
您需要将head
返回的函数传递给pair(1, 2)
返回的函数。所以你需要交换调用函数的顺序:
console.log(pair(1, 2)(head()));
不确定我是否正确理解你的问题,但我猜你正在尝试这样做
const pair = (x, y) => f => f(x, y);
const head = (x, y) => x;
console.log((pair(1,2)(head)))
如果不是,那么上面的这个是正确的
const pair = (x, y) => f => f(x, y);
const head = f => f(a => a);
console.log(head(pair(1,2)))