Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 0.60 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  *  
  3.  *
  4.  *  Fibonacci implemented using the Y comabinator
  5.  *
  6.  *  http://blog.jcoglan.com/2008/01/10/deriving-the-y-combinator/
  7.  *
  8.  *  
  9.  *
  10.  */
  11.  
  12. var Y = function(f) {
  13.     return (function(g) {
  14.         return g(g);
  15.     })(function(h) {
  16.         return function() {
  17.             return f(h(h)).apply(null, arguments);
  18.         };
  19.     });
  20. };
  21.  
  22.  
  23.  
  24. var f = Y(
  25.     function(recurse) {
  26.         return function(n) {
  27.             if(n==0 || n==1) {
  28.                 return 1;
  29.             };
  30.  
  31.             return recurse(n-1) + recurse(n-2);
  32.         };
  33.     }
  34. );
  35.  
  36.  
  37. for(i=0;i<34;i++) {
  38.     f(i);
  39.     //console.log(f(i));
  40. };