title: How to build proper data structure in F#?
body: <p>I'm trying to build a data structure, but I am unsure of how to finish it off.  I am also unsure if I have the right model for this case. I want to take a single list that contains both Primary Keys and Foreign Keys in which to build this structure. The data consist of content related to each other based upon it's rank. For Example:</p>
  
  <pre><code>[{Id = 1; Name = "United States"; Short = "USA"; ParentId = null}
   {Id = 2; Name = "President"; Short = "Pres"; ParentId = 1}
   {Id = 3; Name = "Senate"; Short = "Sen"; ParentId = 2}
   {Id = 4; Name = "House"; Short = "Hou"; ParentId = 2}
   {Id = 5; Name = "State"; Short = "Sta"; ParentId = null}
   {Id = 6; Name = "Governor"; Short = "Gov"; ParentId = 5}
   {Id = 7; Name = "City"; Short = "Cit"; ParentId = null}
   {Id = 8; Name = "Mayor"; Short = "May"; ParentId = 7}]
  </code></pre>
  
  <p>So based upon the above list I should be able to create some sort of a structure based on 4 created lists or sequences. ID 1 should contain ID 2. ID 2 should contain 3 &amp; 4. ID 5 should contain 6, and ID 7 should contain 8.</p>
  
  <p>My model for this:</p>
  
  <pre><code>// Not sure this record is right for the job. Maybe something else like a tree 
  type Government = {
      Id : Id;
      Name : string;
      Abrreviation : string;
      Parent : Government option;
      Child : Government list option
  }
  
  let Select_AllGovStructures () =
  
      let govList = Select.Select_Government.Create(Admin.connectionString).Execute() |&gt; Seq.toList
  
      let createStructure (govList : Select.Select_Government.Record list) =
  
          let parentList =
  
              govList
              |&gt; List.choose (fun x -&gt; 
                                  match x.ParentId.IsNone with
                                  | true -&gt; Some x
                                  | false -&gt; None)
  
          let rec childList (currentList : Select.Select_Government.Record list) 
              (prevParentList : Select.Select_Government.Record list)
              (acc : Government list) =
  
              match (currentList, prevParentList, acc) with
              | [],_,acc -&gt; acc
              | (cl, [], []) when  not cl.IsEmpty -&gt;
  
                  let newCurrentList =
                      currentList
                      |&gt; List.where (fun x -&gt; not (parentList
                                                     |&gt; List.contains x))
  
                  childList newCurrentList parentList
                      (parentList
                      |&gt; List.map (fun x -&gt; {
                                              Government.Id = x.Id;
                                              Name = x.Name;
                                              Abbreviation = x.Abbreviation;
                                              Parent = None;
                                              Child = childList newCurrentList parentList List.empty&lt;Government&gt;
                                          }))
              | (cl, ppL, acc)  -&gt; 
  
                  let parentList =
  
                      cl
                      |&gt; List.choose (fun x -&gt; 
                                          let parent =
                                              ppL |&gt; List.where (fun p -&gt; p.Id = x.ParentId.Value)
                                          match parent.IsEmpty with
                                          | true -&gt; None
                                          | false -&gt; Some x)
                  let newCurrentList =
                      cl
                      |&gt; List.where (fun x -&gt; not (parentList
                                                      |&gt; List.contains x))
  
                  childList cl ppL 
                      (parentList
                      |&gt; List.map (fun x -&gt; {
                                              Government.Id = x.Id;
                                              Name = x.Name;
                                              Abbreviation = x.Abbreviation;
                                              Parent = // Lost! Help!!
                                              Child = // Lost! Help!!
                                          }))
  
          childList govList List.empty&lt;Select.Select_Government.Record&gt; List.empty&lt;Government&gt;   
  
      createStructure govList   
  </code></pre>
  
  <p>I'm new to F# and learning it on my own, so I'm sure that I am probably over complicating things. I've read that trees may be useful in this situation as well as fold or reduce, but I can't quite wrap my head around the solution.  Any advice or help is appreciated.</p>