1

I have problem with my cs homework. I need to access the x value of the function, but my codes is returning me an empty function instead of with the values

I have googled all the currying and closures, but none of them are advanced enough to help me solve my problem

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

my console keeps returning me functions instead when I try all the combinations

function(a,b){return a;}

3 Answers 3

1

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)))

Sign up to request clarification or add additional context in comments.

6 Comments

@Peter here, pair is executed before head. You can add console.log() to check it.
@Peter saw your edit to the question now. Updated head
@Peter I didn't get you. "pass in function as a params" where?
@ adiga I don't get the f(a=> a) part after looking at it for a super long time
a => a is an arrow function just like (x, y) => f. You can write it like (a, b) => a if it's too confusing
|
1

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()));

1 Comment

Then swap the logic in each function.
0

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)))

2 Comments

What does this add? Both of these are already mentioned in the answers above
First one is not same as above but second one yes. I was unable to comment their so mentioned that point here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.