I try to translate this Scheme code to Javascript:
(define (double f)
(lambda (x) (f (f x))))
(define (inc x) (+ x 1))
((double inc) 0)
((double inc) 0)
means (inc (inc 0))
, so it returns 2.
This is my Javascript code:
var double = function(f){
return function(x) { f(f(x)); }
}
var inc = function(x) {return x+1;}
double(inc)(0);
But double(inc)(0)
returns undefined, not 2. Why?
var double = function(f){
return function(x) { return f(f(x)); }
}
var inc = function(x) {return x+1;}
double(inc)(0);
Small mistake :) should work with the return.
If a function doesnt return anything, it actually returns undefined. In your double function you have a function which returns "nothing" => you get undefined.
You missed return
in double
function:
var double = function(f){
return function(x) {return f(f(x)); }
}
var inc = function(x) {return x+1;}
double(inc)(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With