Skip to content

Instantly share code, notes, and snippets.

@roberthoenig
Created March 19, 2018 20:52
Show Gist options
  • Save roberthoenig/68fb3e73d238aa432455499775ec6a4b to your computer and use it in GitHub Desktop.
Save roberthoenig/68fb3e73d238aa432455499775ec6a4b to your computer and use it in GitHub Desktop.
Julia implementation of a Y combinator, together with an exmplae implementation of factorials.
# Define a strict Y combinator function.
Y = (f) -> ((x)->(f((y)->((x(x))(y)))))(
(x)->(f((y)->((x(x))(y))))
)
# Use the Y combinator the implement a factorial function.
factorialbasefunc = (f) -> ((n)->(n==0 ? 1 : n*f(n-1)))
factorial = Y(factorialbasefunc)
println(factorial(5)) # 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment