Fast function of a higher order (a pair of aka cons churches) with typical parameter types that do not allow input parameter types

I was busy with functional programming in Swift 2.1, trying to implement the function the encoding of the / cons pair ( cons = λx λy λf fxy in the untyped lambda calculus ), which I had read could not be done in earlier versions of Swift.

With generics, it looks like

func cons<S,T,U>(x:S,_ y:T) -> ((S,T) -> U) -> U
{
    return { (f:((S,T) -> U)) -> U in return f(x,y)}
}

cons(1,2)
//error: cannot invoke 'cons' with an argument list of type '(Int, Int)'
//note: expected an argument list of type '(S, T)'

which does not work, and gives an error that I cannot understand (of course, a list of parameters of type (Int, Int) can correspond to variables of type generic (S, T)?)

, , , , , 2; , 3 Int (Int, Int) → Int, .

- Any (. Type Casting Any AnyObject), .

? ? , cons/car/cdr, , (lambdas).

+3
1
func cons<S,T,U>(x:S,_ y:T) -> ((S,T) -> U) -> U
{
    return { (f:((S,T) -> U)) -> U in return f(x,y)}
}

let i: ((Int,Int)->Int)->Int = cons(1,2)
let d: ((Int,Int)->Double)->Double = cons(2,3)
let e: ((Double,Int)->String)->String = cons(2.2, 1)
let e: ((Double,Int)->Double)->Double = cons(2.2, 1)

stil "" . , , . ,

func cons<S,T, U>(x:S,_ y:T, outptAs: U.Type) -> ((S,T) -> U ) -> U
{
    return { (f:((S,T) -> U)) -> U in return f(x,y) }
}

let i = cons(1.2 ,"A", outptAs: Int.self)
let j = cons("alfa","beta", outptAs: Double.self)
+1

Source: https://habr.com/ru/post/1615460/


All Articles