Created
March 24, 2017 03:16
-
-
Save ukitaka/257a10565f3bf5a84fe52b8ffe08c2b6 to your computer and use it in GitHub Desktop.
YCombinator.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fix<T, U>(_ f: @escaping (@escaping (T) -> U) -> (T) -> U) -> (T) -> U | |
{ | |
return { f(fix(f))($0) } | |
} | |
let fib: (Int) -> Int = fix { f in { n in if n < 2 { return 1 } else { return f(n - 1) + f(n - 2) } } } | |
fib(1) | |
fib(2) | |
fib(3) | |
fib(4) | |
let factorial: (Int) -> Int = fix { f in { n in if n == 1 { return 1 } else { return n * f(n - 1) } }} | |
factorial(1) | |
factorial(2) | |
factorial(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment