TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Y Combinator in Python

8 pointsby technoguyrobover 16 years ago

6 comments

silentbicycleover 16 years ago
Of course, if you actually <i>use</i> this you can smash the stack -- Python doesn't currently have any kind of tail call optimization. (That factorial function isn't tail-recursive anyway, though.)<p>It doesn't look like Ruby has tail-call optimization, either. Lua does...what other primarily-scripting (i.e., not Scheme or Haskell) languages do?
burkeover 16 years ago
<p><pre><code> # Ruby 1.8.7 def Y lambda { |f| f.call(f) }.call( lambda do |g| yield(lambda { |*n| g.call(g).call(*n) }) end) end Y { |this| lambda { |n| n == 0 ? 1 : n * this.call(n - 1) } }.call(5) #=&#62; 120</code></pre>
pavelludiqover 16 years ago
The Y combinator is just like python decorators, or generators, you just HAVE to blog about them, or you're not C001!
markessienover 16 years ago
This must be about the 6th different 'Y Combinator in X' I have seen on this site.
daraghover 16 years ago
That's a pretty solid butchering of the design of daringfireball.net
kingkongrevengeover 16 years ago
Perl6 ftw:<p>my $factorial = sub (Int $x) { $x &#60; 2 ?? 1 !! $x * &#38;?ROUTINE($x - 1); }