键入函数的推断作为参数

Art*_*tur 5 f#

我想写一个函数,它接受几个tupples作为参数,并选择他们的元素并传递给另一个函数,其中我作为另一个参数给出.我试过这样的事:

let function (tup1:'A*'A) (tup2:'B*'B) i =
    otherFunction (i tup1) (i tup2)
function Tup1 Tup2 fst
Run Code Online (Sandbox Code Playgroud)

我有一个错误,因为i预计'A*'A ->'A不会'B*'B->'B.是否可以使此代码工作?

提前致谢.

kvb*_*kvb 4

您基本上想要传递 类型的参数\xe2\x88\x80\'a.\'a*\'a->\'a,但在 F#(和其他 ML)中,仅支持1 级多态性,因此您不能直接执行此操作。解决方法是使用通用方法定义新类型来模拟更高级别的多态性:

\n\n
type Untupler =\n    abstract Apply : \'a*\'a -> \'a\n\nlet myFunction tup1 tup2 (i:Untupler) =\n    otherFunction (i.Apply tup1) (i.Apply tup2)\n\nmyFunction Tup1 Tup2 { new Untupler with member __.Apply (x,y) = x }\n
Run Code Online (Sandbox Code Playgroud)\n