Skip to content

Instantly share code, notes, and snippets.

@fcanas
Last active August 29, 2015 14:16
Show Gist options
  • Save fcanas/d541b089a03956ae5515 to your computer and use it in GitHub Desktop.
Save fcanas/d541b089a03956ae5515 to your computer and use it in GitHub Desktop.
Swift Church Encoding. Barely.
typealias ø = ((Int -> Int) -> Int -> Int)
func i<T>(v :T) -> T {
return v
}
func c<T>(n: Int) -> (T -> T) -> T -> T {
if n == 0 {
return { x in i }
}
return { (𝑓 : (T -> T)) -> T -> T in
return { x in
return 𝑓(c(n - 1)(𝑓)(x))
}
}
}
func u<T>(f : ((Int -> Int) -> Int -> T)) -> T {
return f({ (i : Int) -> Int in
return i + 1
})(0)
}
let o :ø = c(0)
let e :ø = c(1)
let ee :ø = c(2)
let eee :ø = c(3)
u(e) // 1
u(ee) // 2
u(eee) // 3
u(c(4)) // 4
u(c(5)) // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment