Live data from Hacker News

Recursions without names: Introduction to the Y combinator in JavaScript

blog.klipse.tech

11–20 of 33 posts

Re: Recursions without names: Introduction to the Y combinator in JavaScript

#11
post #4

For what purpose?

Quoth xkcd, "Tail recusion is its own reward." In all seriousness, even if the y combinator is practically useless in languages that don't have proper TCO (which is specced for ES7), it's a useful learning tool, is powerfully mind expanding, and can help to demonstrate the power of the λ-calculus.

There is a practical application for recursive memoization:

http://blog.klipse.tech/lambda/2016/08/10/y-combinator-app-j...

Re: Recursions without names: Introduction to the Y combinator in JavaScript

#12

If I'm not mistaken, this isn't the full Y combinator yet. It's a partial step towards deriving the full Y combinator. (f => (x => x(x))(y => f(x => y(y)(x)))) (func => n => (n === 0) ? 1 : (n * func(n - 1))) (19) Notice that the ugly func(func)(n-1) recursive call with the proper Y combinator simply became func(n-1). The full Y combinator is also vastly more mystical and less obvious than the partial step :)

The full Y combinator is presented in the sequel of the current article: http://blog.klipse.tech/lambda/2016/08/10/pure-y-combinator-...

Re: Recursions without names: Introduction to the Y combinator in JavaScript

#13
post #4

For what purpose?

Quoth xkcd, "Tail recusion is its own reward." In all seriousness, even if the y combinator is practically useless in languages that don't have proper TCO (which is specced for ES7), it's a useful learning tool, is powerfully mind expanding, and can help to demonstrate the power of the λ-calculus.

http://xkcd.com/1270/

Re: Recursions without names: Introduction to the Y combinator in JavaScript

#14
post #4

For what purpose?

Quoth xkcd, "Tail recusion is its own reward." In all seriousness, even if the y combinator is practically useless in languages that don't have proper TCO (which is specced for ES7), it's a useful learning tool, is powerfully mind expanding, and can help to demonstrate the power of the λ-calculus.

Is the Y-combinator any more or less dependent on TCO than regular recursion?

The Y combinator is useless in languages that already have builtin support for recursive functions.

Re: Recursions without names: Introduction to the Y combinator in JavaScript

#18
post #14

Earlier quoted context omitted.

Quoth xkcd, "Tail recusion is its own reward." In all seriousness, even if the y combinator is practically useless in languages that don't have proper TCO (which is specced for ES7), it's a useful learning tool, is powerfully mind expanding, and can help to demonstrate the power of the λ-calculus.

Is the Y-combinator any more or less dependent on TCO than regular recursion? The Y combinator is useless in languages that already have builtin support for recursive functions.

Not useless, almost useless:

You can do elegant recursive memoization with the Y combinator as explained here http://blog.klipse.tech/lambda/2016/08/10/y-combinator-app-j...

Re: Recursions without names: Introduction to the Y combinator in JavaScript

#19

I find the Python implementation is slightly more elegant: (lambda f: f(f)) (lambda fn: lambda n: 1 if n == 0 else (n * fn(fn)(n - 1))) (5)

Yeah. But I didn't find a way to run python code in the browser. Currently in Klipse, we support javascript, ruby, clojure and php...

Re: Recursions without names: Introduction to the Y combinator in JavaScript

#20

The code samples fail in Safari. The first one prints "SyntaxError: Unexpected token '>'".

Your version of Safari most likely lacks support for ES6 features like arrow functions[0], you could use babel[1] to transpile the code to ES5:

  (function (f) {
    return f(f);
  })(function (func) {
    return function (n) {
      return n === 0 ? 1 : n * func(func)(n - 1);
    };
  })(19);

[0] http://caniuse.com/#feat=arrow-functions

[1] https://babeljs.io/repl/

Post reply on HN