YCombinator not working in Swift

牧云@^-^@ 提交于 2019-12-03 17:18:03

There's a great post by xiliangchen that walks through creating a Y-combinator in Swift. (Technically, this isn't a Y-combinator, since it is explicitly recursive, but it largely does what you want.) Here's an example of that Y function (stripped of its generic specification for clarity):

typealias G = Int -> Int

func Y (f: G -> G) -> G {
    return {
        (i: Int) -> Int in
        f(Y(f))(i)
    }
}

let factorial = Y { (f: G) -> G in
    { (n: Int) -> Int in
        if n == 0 {
            return 1
        } else {
            return n * f(n - 1)
        }
    }
}

factorial(5)        // 120

For more on Y-combinators, you can look at this terrific (long) piece by Mike Vanier.

(Note: Using Any is kind of a mess -- I'd recommend steering clear of it whenever you can, especially since you don't need it in this case.)

You can implement a real (without explicit recursion) Y combinator using a recursive type, without any unsafe tricks (credits to Rosetta Code):

struct RecursiveFunc<F> {
  let o : RecursiveFunc<F> -> F
}

func Y<A, B>(f: (A -> B) -> A -> B) -> A -> B {
  let r = RecursiveFunc<A -> B> { w in f { w.o(w)($0) } }
  return r.o(r)
}

let factorial = Y { (f: Int -> Int) -> Int -> Int in
  { $0 <= 1 ? 1 : $0 * f($0-1) }
}
println(factorial(10))

Any doesn't really help because Any cannot represent function types.

Update: Starting in Xcode 6.1 beta 3, Any can represent function types, and your code compiles and works correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!