Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# op_加法的静态成员约束_F# - Fatal编程技术网

F# op_加法的静态成员约束

F# op_加法的静态成员约束,f#,F#,为了便于学习,我正在尝试使用无点内联函数重新创建列表。折叠,列表。减少,和列表。求和。以下是我所拥有的: let flip f y x = f x y let rec fold folder seed = function | [] -> seed | h :: t -> fold folder (folder seed h) t let inline reduce< ^a when ^a : (static member Zero : ^a) > :

为了便于学习,我正在尝试使用无点
内联
函数重新创建
列表。折叠
列表。减少
,和
列表。求和
。以下是我所拥有的:

let flip f y x = f x y

let rec fold folder seed = function
    | [] -> seed
    | h :: t -> fold folder (folder seed h) t

let inline reduce< ^a when ^a : (static member Zero : ^a) > : 
    (^a -> ^a -> ^a) -> ^a list -> ^a =
    flip fold LanguagePrimitives.GenericZero

let inline sum< ^a when 
            ^a : (static member (+) : ^a * ^a -> ^a) and 
            ^a : (static member Zero : ^a)> : ^a list -> ^a = 
    reduce (+)
           // ^ Compile error here

我应该添加什么来满足编译器的要求?

我不知道如何完全按照您的要求执行,因为我不知道如何以无点样式调用静态约束的静态成员。请注意,您没有调用
^a
(+)
,编译器不知道如何解决这个问题。。。如果您乐于使用内部帮助器函数,则可以执行以下操作:

let inline sum< ^a
    when ^a : (static member (+) : ^a -> ^a -> ^a) and
         ^a : (static member Zero : ^a) > : ^a list -> ^a =
    let add x y = (^a : (static member (+) : ^a -> ^a -> ^a) (x, y))
    reduce add

与vandroy一样,给定的reduce函数不适合我编译。我认为您想要的
reduce
功能实际上是:

let inline reduce< ^a when ^a : (static member Zero : ^a) > : ((^a -> ^a -> ^a) -> ^a list -> ^a)  =
    flip fold LanguagePrimitives.GenericZero
试图调用
sum[1..10]
会导致
类型“int”不支持运算符“get\u op\u Addition”
。所以我不确定这种方法是否有效

编辑:感谢CaringDev的提示,我修复了get_op_加法错误。这似乎奏效了。至于为什么,我很困惑,因为
'b
只是伪装成
^a

let inline sum< ^a when 
        ^a : (static member Zero : ^a) and
        ^a : (static member (+) : ^a -> ^a -> ^a)  > : ^a list -> ^a =
    let inline add (x:'b) (y:'b) = (+) x y
    reduce add
让内联求和<^a
^a:(静态成员零:^a)和
^a:(静态成员(+):^a->^a->^a)>:^a列表->^a=
让内联加法(x:'b)(y:'b)=(+)xy
减少增加

我无用的回答是:不要那样做。内联定义仅用于语法函数,因此不应以无点方式定义它们。例如,比较尝试评估以下三行的结果:

let inline identity = id
let inline identity : ^a -> ^a = id
let inline identity< ^a> : ^a -> ^a = id

我似乎无法重现这个问题,因为即使
reduce
也不能为我编译。我得到了预期的
'a->'b->'a->'a->'a
,但给定
('c->'d->'c->'d list->'c
类型
'a
与指向foldBTW的类型
'b->'c->'b
不匹配,list.reduce的F#实现在空列表上失败,不需要一般零。例如,您可以这样定义reduce:let rec reduce op=function |[]->failwith“empty”|[x]->x | h::t->op h(reduce op t)@Sehnsucht很抱歉,这是我的打字错误,我添加了类型注释来修复
reduce
(又称Vandroy)的编译错误我似乎有了一个新的昵称^^Vandroy发布了答案,然后他删除了它,所以现在你必须满足了哈哈。这个错误源于
+
约束的多余括号:这些使编译器相信你想让一个成员获得(即返回)
+
函数。@CaringDev直到那个(+)约束我才成功;这不太可能是一个非类型化的代码给我的error@Sehnsucht,是的,你的是由于问题中的一个错误。我开始删除所有注释。。。所以我甚至没有注意到:-)@Ringils
类型“int”不支持…
是由于
静态成员(+):(^a->^a->^a)
而不是
静态成员(+):^a->^a->^a
let inline sum< ^a when 
        ^a : (static member Zero : ^a) and
        ^a : (static member (+) : (^a -> ^a -> ^a))  > : ^a list -> ^a = 
    reduce (fun i j -> i)
let inline sum< ^a when 
        ^a : (static member Zero : ^a) and
        ^a : (static member (+) : ^a -> ^a -> ^a)  > : ^a list -> ^a =
    let inline add (x:'b) (y:'b) = (+) x y
    reduce add
let inline identity = id
let inline identity : ^a -> ^a = id
let inline identity< ^a> : ^a -> ^a = id
let inline reduce< ^a, 'b  when  ^a : (static member Zero : ^a)> : ( ^a -> 'b ->  ^a) -> ('b list ->  ^a) =
    flip fold LanguagePrimitives.GenericZero

let inline (+) (x:^a) (y:^a) = x + y

let inline sum< ^a
                 when ^a : (static member ( + ) :  ^a * ^a -> ^a)
                  and ^a : (static member Zero : ^a)> : ^a list -> ^a =
    reduce (+)