Hacker News new | past | comments | ask | show | jobs | submit login

Just out of curiosity I've made a bug-to-bug translation of original Ruby version of the Y Combinator function from the submitted link to Python language.

In Ruby http://is.gd/gdJ :

  y = proc { |generator|
    proc { |x|
        proc { |*args|
            generator.call(x.call(x)).call(*args)
        }
    }.call(proc { |x|
        proc { |*args|
            generator.call(x.call(x)).call(*args)
        }
    })
  }
  
In Python:

  # Y = λf·(λx·f (x x)) (λx·f (x x))
  y = lambda generator:\
    (lambda x: lambda *args: generator(x(x))(*args))\
    (lambda x: lambda *args: generator(x(x))(*args))
Example with factorial:

  factorial_generator = lambda callback: lambda n: n*callback(n-1) if n > 1 else 1
  factorial = y(factorial_generator)
  n = 5
  print "%d! = %d" % (n, factorial(5))



Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: