I was looking at this site: http://rosettacode.org/wiki/Fibonacci_sequence#JavaScript and saw this program:
function fib(n) {
return function(n,a,b) {
return n>0 ? arguments.callee(n-1,b,a+b) : a;
}(n,0,1);
}
How does this work, what are those two arguments (a and b) help. I traced it and still can’t figure out how this works
In the
function(n,a,b),nserves as a countdown counter, andabstores two consecutive Fibonacci numbers for the purpose of computing the next, so whennreaches 0, you haveaas the n+1-th Fibonacci number (since the real Fibonacci has two 1s as the beginning).E.g., n=4:
As you can see, the value of a and b always equal to the Fibonacci numbers. Also, this is very similar to Functional Programming (as the website stated Scheme programmers).