为什么参数b在此示例中具有类型a?

时间:2017-07-14 19:09:49

标签: f#

我正在调查aether library并注意到这一点

 type Lens<'a,'b> =
    ('a -> 'b) * ('b -> 'a -> 'a)

 static member (^=) (Set, (_, s): Lens<'a,'b>) =
            fun (b: 'b) ->
                s b : 'a -> 'a

在^ =函数中,b参数如果是lambda中的类型&#; b。但是,在lamda体中,为什么b类型现在是?

2 个答案:

答案 0 :(得分:4)

  

然而,在lamda体中,为什么b型是'a now?

不是。

'b是输入的fun (b: 'b) ->,如static member (^=) (Set, lens: Lens<'a,'b>) = // Pattern match to extract out the 2nd portion of the lens, which is a function: 'b -> 'a -> 'a let (_,s) = lens // Define a function that takes a 'b and returns a new function of type: 'a -> 'a let fn (b: 'b) : 'a -> 'a = s b // this just partially applies s with the input "b" fn // Return the function 所示。

我们可以在没有匹配的情况下重写该成员,并使用本地定义的函数,如下所示:

(Set, (_,s))

基本上,参数列表中的Lens<'a,'b>将“s”绑定到('b -> 'a -> 'a)的第二部分,或者键入 var stored = +localStorage.getItem('boxvalue'); 的函数。在上面,我已经将其解释为更明确,并在自己的绑定中完成了这个提取。

然后该成员返回一个本地定义的函数(作为lambda)。上面,我重写了使用let绑定函数,因为它通常更清晰。

答案 1 :(得分:4)

我认为你只是误读了语法。代码s b : 'a -> 'a并不意味着b属于'a类型。

阅读它的正确方法是将其分为两部分:冒号前的部分是表达式,冒号后面的部分是该表达式的类型。

因此,代码s b : 'a -> 'a实际上意味着s b属于'a -> 'a类型。它没有单独说出sb类型的任何内容。

相关问题