Function returns value without replacing the variable with the given parameter
-
01-07-2021 - |
Question
sry about the stupid way I phrased the question here's some explanation: I am experimenting with lambda-calculus in javascript and I am having some minor difficulties. (you don't have to know anything about lambda-calculus to help me)
I have this function (the church numeral 1 btw):
function num1(c) {
return function(x){
return c(x);
}
}
alert(num1)
Behaves as expected and gives the exact same thing as above.
alert(num1(num1))
Behaves unexpected and gives:
function (x) {
return c(x);
}
Why doesn't the javascript replace the 'c' with the function num1? but
alert(num1(num1)(num1))
Gives:
function (x) {
return c(x);
}
And shows that the first c was in fact replaced by the function as it was supposed to. If the 'c' would not have been replaced, then this would have happened:
(num1(num1)(num1))=
(function (x) {return c(x);}(num1=
c(function num1(c) {
return function(x){
return c(x);
}
})
So all in all, the code is doing what is it supposed to, but it doesn't output the function with the 'c' replaced. What can I do? Later, I will more functions and then I wont be I able to tell num1(asd)
and num1(jkl)
apart because the 'c' doesn't get replaced.
Thank you very much for your help!
Someonelse
Solution
Why don't you try this:
function num1(c) {
function rv(x){
return c(x);
}
rv.showBinding = function() {
return c;
}
return rv;
}
Then:
alert(num1);
alert(num1(num1).showBinding());