如何使用 seq.groupby 和 seq.fold 创建树(F#)?

问题描述 投票:0回答:0

这失败了(可以做到吗?):

假设,我有一棵树(要在 WPF 中显示)为:

type BranchDate = {Id: Guid; PostingDate: DateTime}

type LedgerRecord = {        
        rid: Guid                       
        DebitCredit: string option      
        PostingTime: DateTime option
        BillTo: string option           
        CptCode: string option
        Description: string option
        Charge: double option
        CheckNum: string option
        PaymentMethod: string option     
        Paid: double option
        Refiled: double option           
        EncounterInsuranceId : int option
        CptId: int option
    }

type Tree =
  | Branch of BranchDate * Tree list
  | Leaf of LedgerRecord

LederRecords 从数据库下载为

seq<LedgerRecords>.

我的最大努力失败了

 let MakeTree (p:LedgerRecord[] option) : Tree = 


  let insert tree  = raise <| NotImplementedException()


  let makeBranch (d:DateTime) (v:seq<LedgerRecord>) : Tree = 
     Branch ({Id = Guid.NewGuid(); PostingDate = d}, v |> Seq.map (fun q -> Leaf q) |> Seq.toList)

  let root = Branch ( {Id=Guid.NewGuid(); PostingDate = new DateTime(01,01,01)}, [] )
  
  let ggg = 
            match p with
            | None -> failwith "Branch ( {Id=Guid.NewGuid(); PostingDate = new DateTime(01,01,01)}, [] )"
            | Some pp -> pp 
                            |> Seq.groupBy (fun t -> Option.get t.PostingTime)
                            |> Seq.map (fun (k,v) -> makeBranch k v)
   
  let hhh = Seq.fold insert root ggg  
  hhh

我不知道如何编写生成树的“插入”。在 Wpf 中,树将显示为:

1/1/1919
      shirt
      pants
      shoes
2/3/1956
      carpet
      heater
      dog food

非常感谢任何帮助。

蒂亚

f# tree
© www.soinside.com 2019 - 2024. All rights reserved.