Nested anonymous functions

Consider the following functions

const zero = f => a => a;
const succ = n => f => a => f(n(f)(a));
const jsnum = n => n(x => x +1)(0);

jsnum(succ(zero)); // 1

What does the (0) at the end of jsnum mean?

it is a function argument

this code is a mess. difficult to follow… a nightmare to debug…

it is possible, but it is still a mess.

1 Like

also, this doesn’t return 1 as it say as there is a syntax error
I image you wanted jsnum to be a variable, so why there is the arrow after it?

write it like this instead

const zero = f => a => a;
const succ = n => f => a => f(n(f)(a));
const jsnum = n => n(x => x +1)(0);

jsnum(succ(zero)); // 1

and you can use this to see what happens:
http://pythontutor.com/javascript.html

it is still pretty cryptic, but if you add together this and writing on paper what happens at each step you should be able to follow

I fixed the error in the original code. It is a lambda calculus exercise where you can derive numbers from pure functions.

It’s not related to the exercises and I’m just not familiar with the notation in jsum. I’m guessing that zero is the second parameter of what appears to be a curried function.