检索最常用的名称

时间:2012-12-11 12:08:12

标签: f#

我无法理解。如何找到最常用的名称,例如

type person = {name:string; surname:string}

let persons=[{name:"Jack";surname:"YYY"};
             {name:"Joe";surname:"XYX"};
             {name:"Jack";surname:"XXY"}]

1 个答案:

答案 0 :(得分:7)

// You have to use '=' instead of ':' between field names and field values
let persons = 
    [ {name = "Jack"; surname = "YYY"};
      {name = "Joe"; surname = "XYX"};
      {name = "Jack"; surname = "XXY"} ]

// val it : string = "Jack"
persons |> Seq.countBy (fun {name = s} -> s)
        |> Seq.maxBy snd
        |> fst