[英]How to define a zero element when using discriminated unions in F# Array functions
OK, a question, I would like to use an array of discriminated unions in the Array functions. 好的,一个问题,我想在Array函数中使用一组有区别的联合。 In the code below, I define a type ResultVari
that is either Unknown
or a floating point value. 在下面的代码中,我定义了一个类型ResultVari
,它是Unknown
或浮点值。 I also define the infix plus operator for the type, which returns a value only if both args are not Unknown
. 我还为类型定义了infix plus运算符,仅当两个args都不是Unknown
时才返回值。 This works fine. 这很好用。
type ResultVari =
| Unknown
| Value of float
static member (+) (a,b) = // add two ResultVari's together
match a,b with
| Value(av),Value(bv) -> Value(av + bv) // only has a value if both args do.
| _ -> Unknown
(* Summation of array of ResultVari, such that if any are unknown then the result is Unknown *)
let example1 = [| Value(4.0); Value(5.5); Value(3.1) |] // summation should be 12.6
let example2 = [| Value(4.0); Unknown; Value(3.1) |] // summation should be Unknown
let summation (varArray:ResultVari array) =
Array.sum (+) varArray //ERROR this value is not a function and cannot be applied
let summation2 (varArray:ResultVari array) =
Array.fold (+) (Value(0.0)) varArray // Works
let sum_example1 = summation2 example1
let sum_example2 = summation2 example2
printfn "%A" sum_example1 // 12.6
printfn "%A" sum_example2 // Unknown
Using summation2
the program works as expected with the sum for example1
being 12.6 and for example2
being Unknown
. 使用summation2
,程序按预期工作, example1
的总和为12.6, example1
example2
为Unknown
。
But I don't understand why summation
doesn't work - the compiler complains "this value is not a function and cannot be applied". 但我不明白为什么summation
不起作用 - 编译器抱怨“这个值不是函数而且不能应用”。 In another attempt (not shown), I also got the error of a missing get_Zero element which I understand -- the sum function has to use some type of a zero definition to start the summation, and using the fold function with my Value(0.0) as the start value like in summation2
solves that. 在另一次尝试(未显示)中,我也得到了一个缺失的get_Zero元素的错误,我理解 - sum函数必须使用某种类型的零定义来开始求和,并使用fold函数和我的值(0.0) )如summation2
起始值解决了这个问题。
So is there a way to define a get_Zero element for a discriminated union, or would I have to use a record type instead for ResultVari? 那么有没有办法为有区别的联合定义get_Zero元素,或者我必须使用记录类型代替ResultVari? Then I could use Array.sum instead of using Array.fold. 然后我可以使用Array.sum而不是使用Array.fold。
You need to add a zero
element to use array.sum - like this: 你需要添加一个zero
元素来使用array.sum - 像这样:
type ResultVari =
| Unknown
| Value of float
static member (+) (a,b) = // add two ResultVari's together
match a,b with
| Value(av),Value(bv) -> Value(av + bv) // only has a value if both args do.
| _ -> Unknown
static member Zero with get() = Value(0.0)
Then the code becomes: 然后代码变成:
let summation (varArray:ResultVari array) =
Array.sum varArray
This makes sense because when you sum something you need to start from zero and without the zero member, the compiler doesn't know where to start. 这是有道理的,因为当你总结一些你需要从零开始而没有零成员时,编译器不知道从哪里开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.