Swift高阶函数(Church对aka cons),其泛型参数类型不接受输入参数类型 [英] Swift higher order function (Church pair aka cons) with generic parameter types not accepting input parameter types

查看:269
本文介绍了Swift高阶函数(Church对aka cons),其泛型参数类型不接受输入参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift 2.1中编写函数式编程,试图实现教会编码对/利弊功能(缺点 =λxλyλffxy in untyped lambda微积分),我有阅读无法在早期版本的Swift中完成。使用

org / wiki / Generic_programmingrel =nofollow>泛型它看起来像

  func cons (x:S,_y:T)→> ((S,T)→U)→> U 
{
return {(f:((S,T) - > U)) - > U返回f(x,y)}
}

cons(1,2)
//错误:无法用类型为'(')的参数列表调用'cons' (S,T)'


$ b的参数列表$ b

这不起作用,并给出了我无法理解的错误(当然,类型(Int,Int)的参数列表可以匹配泛型类型变量(S,T)?)

如果抛弃泛型,并将它们全部声明为Ints,则该函数可以工作,但我们当然希望能够将长度大于2的列表放在一起;例如,包含一个长度为3的列表是一个Int与一个(Int,Int) - > Int。

另一种选择是将所有内容输入为任何(参见类型为Any和AnyObject的转换),但我无法做到这一点。



<你有什么想法吗?这可能在斯威夫特呢?我确信有更简单的方法可以实现 cons / car / cdr ,但我对Church编码特别感兴趣,列表元素是匿名函数(lambdas)的参数。

解决方案

  func cons (x:S,_ y:T) - > ((S,T)→U)→> U 
{
return {(f:((S,T) - > U)) - > U(返回f(x,y)}
}

让我:((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)



其中一种类型是额外类型,不能由编译器推断。如果你定义了类型,你可以看到,并非所有的组合都是有效的。只需定义输出类型,编译器应该很开心

  func cons (x:S,_) y:T,outptAs:U.Type) - > ((S,T)→U)→> U 
{
return {(f:((S,T) - > U)) - > U(换言之,f(x,y)}
}

让我= cons(1.2,A,outptAs:Int.self)
let j = cons( alfa,beta,outptAs:Double.self)


I was messing around with the functional programming in Swift 2.1, trying to implement the Church encoding pair/cons function (cons = λx λy λf f x y in untyped lambda calculus), which I had read couldn't 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 doesn't work, and gives an error I cannot understand (surely parameter list of type (Int,Int) can match generic type variables (S,T)?)

If you get rid of the generic types, and declare them all Ints, the function works, but of course we want to be able to cons together lists longer than 2; consing a list of length 3 is consing an Int with an (Int,Int) -> Int, for example.

Another option is to type everything as Any (see Type Casting for Any and AnyObject), but I couldn't make that work either.

Do you have any ideas? Is this possible in Swift yet? I'm sure there are simpler ways to implement cons/car/cdr, but I'm specifically interested in the Church encoding, where the list elements are arguments to anonymous functions (lambdas).

解决方案

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 one of type is an 'extra' type and could not be inferred by compilator. if you define the types, you can see, that not all combinations are valid. Just define the output type and the compilator should be happy

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)

这篇关于Swift高阶函数(Church对aka cons),其泛型参数类型不接受输入参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆