Confused about javascript function returning multiple functions with many fat arrow

萝らか妹 提交于 2019-12-01 12:44:30

You could change head function like this:

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;}

Let's make this easier to read. In ES5, your code looks like this:

var pair = function(x, y) {
  return function(f) {
    return f(x, y);
  }
};

var head = function(p) {
  return function(a, b) {
    return a;
  }
};

You need to pass the function returned from head to the function returned by pair(1, 2). So you need to swap which order you're calling the functions in:

console.log(pair(1, 2)(head()));
Milind Agrawal

Not sure if I understand your question correctly or not but I guess you are trying to do this

const pair = (x, y) => f => f(x, y); 
const head = (x, y) => x;

console.log((pair(1,2)(head)))

if not then this one from above is correct

const pair = (x, y) => f => f(x, y); 
const head = f => f(a => a);

console.log(head(pair(1,2)))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!