区分联合列表F#的两个值的总和

Jac*_*ski 4 f# discriminated-union

我有运动建议从区别联合列表中加入相同类型的每个值的函数,如:

type volume =
     | Litre of float
     | Galon of float
     | Bucket of float
     | Bushel of float

let list = [Litre(20.0);Litre(30.0);Galon(2.0);Bucket(5.0);Litre(5.0);Galon(3.0)];
Run Code Online (Sandbox Code Playgroud)

输出应该如下所示:

[Litre(55.0);Galon(5.0);Bucket(5.0)]
Run Code Online (Sandbox Code Playgroud)

我已经找到了部分解决方案:

let rec sumSameTypes (list:volume list) =
    match list with
    | a::b::t -> if a.GetType() = b.GetType() then // and there is part where I don't know how to sum two of these elements
    | [] -> failwith "EMPTY"
Run Code Online (Sandbox Code Playgroud)

Tom*_*cek 7

由于这看起来更像是一个学习问题,我将尝试给出一些提示,而不是一个完整的答案.

虽然GetType可以(在这种情况下)用于检查两个有区别的联合值是否具有相同的情况,但这不是特别的功能样式.您需要使用模式匹配来检查您所拥有的案例,这也允许您提取数值:

match a with
| Litre(n) -> // Do something with 'n'
// Add all the other cases here
Run Code Online (Sandbox Code Playgroud)

我认为首先要考虑的是你想得到什么结果 - 我想最容易的选择是得到四个数字代表升,加仑,桶和蒲式耳的总数.

let rec sumSameTypes (list:volume list) : float * float * float * float =
    match list with
    | [] -> 
        // For empty list, we just have 0 of everything
        0.0, 0.0, 0.0, 0.0
    | x::xs ->
        // For non-empty list, process the rest recrsively
        // and pattern match on `x` to figure out which of 
        // the numbers you need to increment
Run Code Online (Sandbox Code Playgroud)

这是非常基本的方法,但我认为这是开始使用的最佳方式.通常,您可能会使用类似地图的东西(从单位到值),但是使用看起来更像的类型也是有意义的:

type Unit = Litre | Galon | Bushel | Bucket
type Volume = { Amount : float; Unit : Unit }
Run Code Online (Sandbox Code Playgroud)

这样可以更容易,因为您可以使用它Map<Unit, float>作为结果.