Y Combinator in Python(akropolix.net) |
Y Combinator in Python(akropolix.net) |
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?
# 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)
#=> 120my $factorial = sub (Int $x) { $x < 2 ?? 1 !! $x * &?ROUTINE($x - 1); }