Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The Y combinator allows the definition of anonymous recursive functions. These have names ("fac").

Personally, I have never found much of a reason to use the Y combinator. I typically use the method you used in the second example; I declare a named function within the scope of the anonymous one. I find it to be a readability win.



You can still do a Y combinator in JavaScript. In http://www.mail-archive.com/boston-pm@mail.pm.org/msg02716.h... I not only show how to do it, but I show one way that someone reasonably could come up with the construct.

But I agree with you that Y combinators are not generally a good approach for working programmers because there are clearer ways of accomplishing the same thing. However I've heard that in some functional languages they can be very useful as a pattern to compile language constructs to.


I also did something like this. It's fun to port lisp constructs to Javascript.

http://foohack.com/tests/ycomb.html


Does this count as an anonymous recursive function?

a = (function(x) {return x == 0 ? 1 : x * arguments.callee(x - 1)})(8);


Full anonimity, thanks to tilly (separated for better understanding):

  builder   = function(x){ return function(n){ return x(x)(n); }}
  factorial = function(f){ return function(n){ return n==0?1:n*f(f)(n-1);} }
  alert(builder(factorial)(8))
One liner, just seed factorial to builder:

  r = (function(x){ return function(n){ return x(x)(n); } } (function(f){ return function(n){ return n==0?1:n*f(f)(n-1);} }))(8);




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: