Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created September 21, 2013 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/6647263 to your computer and use it in GitHub Desktop.
Save joyrexus/6647263 to your computer and use it in GitHub Desktop.
Y-combinator
# http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator
#
# λf . (λx . x x)(λy . f (λv . ((y y) v)))
Y = (f) -> ((x) -> x x)((y) -> f ((v) -> (y y) v))
# or ...
Y = (f) ->
p = (h) ->
(x) -> f(h(h))(x)
p p
fib = (f) ->
(n) -> if n == 0 then 1 else n * f(n - 1)
console.log Y(fib) i for i in [0 .. 6] # 1 1 2 6 24 120 720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment