Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
17
Task/Y-combinator/Swift/y-combinator-1.swift
Normal file
17
Task/Y-combinator/Swift/y-combinator-1.swift
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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 fac = Y { (f: Int -> Int) in
|
||||
{ $0 <= 1 ? 1 : $0 * f($0-1) }
|
||||
}
|
||||
let fib = Y { (f: Int -> Int) in
|
||||
{ $0 <= 2 ? 1 : f($0-1)+f($0-2) }
|
||||
}
|
||||
println("fac(5) = \(fac(5))")
|
||||
println("fib(9) = \(fib(9))")
|
||||
5
Task/Y-combinator/Swift/y-combinator-2.swift
Normal file
5
Task/Y-combinator/Swift/y-combinator-2.swift
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
func Y<A, B>(f: (A -> B) -> A -> B) -> A -> B {
|
||||
typealias RecursiveFunc = Any -> A -> B
|
||||
let r : RecursiveFunc = { (z: Any) in let w = z as! RecursiveFunc; return f { w(w)($0) } }
|
||||
return r(r)
|
||||
}
|
||||
3
Task/Y-combinator/Swift/y-combinator-3.swift
Normal file
3
Task/Y-combinator/Swift/y-combinator-3.swift
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
func Y<In, Out>( f: (In->Out) -> (In->Out) ) -> (In->Out) {
|
||||
return { x in f(Y(f))(x) }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue