[erlang-questions] Auto generated functions

Richard Carlsson richardc@REDACTED
Thu Feb 21 16:21:40 CET 2008


Zvi wrote:
> In Javascript, unlike Erlang, anonymous function can call itself, without
> using Y-combinator tricks.
> Does it mean that JS is more functional than Erlang:
> 
> factorial = function(n){ return (n==0) ? 1 : n*arguments.callee(n-1); };

No, it means that JS is more *Object Oriented* than Erlang.

But in Erlang, you don't need to use any "Y-combinator tricks":
simply pass the function itself as an extra argument.

  FactX = fun(0, F) -> 1;
             (N, F) -> N * F(N-1, F)
          end,
  13 = FactX(6, FactX)

In practice, this is also how the self-reference works in JS. The
extra parameter is an added cost, that you don't have in Erlang
unless you actually need it (which is not often).

     /Richard






More information about the erlang-questions mailing list