Skip to content

Instantly share code, notes, and snippets.

@kongtomorrow
Created July 9, 2014 18:30
Show Gist options
  • Save kongtomorrow/cbe2d73075e879f7d73d to your computer and use it in GitHub Desktop.
Save kongtomorrow/cbe2d73075e879f7d73d to your computer and use it in GitHub Desktop.
recursion with an unnamed lambda via fancy pants fixed point combinator
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