Created
July 9, 2014 18:30
-
-
Save kongtomorrow/cbe2d73075e879f7d73d to your computer and use it in GitHub Desktop.
recursion with an unnamed lambda via fancy pants fixed point combinator
This file contains 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 fixedPoint<S,T>(f:(S->T) -> (S->T)) -> (S->T) { | |
return { | |
(x : S) -> T in | |
return f(fixedPoint(f))(x) | |
} | |
} | |
let fact:(Int)->Int = fixedPoint { | |
(almostFactorial:(Int)->Int) -> ((Int)->Int) in | |
return { | |
(n : Int) -> Int in | |
if n == 0 { | |
return 1 | |
} else { | |
return n * almostFactorial(n-1); | |
} | |
} | |
} | |
println("\(fact(5))") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment