This takes a list of categories and organizes them into a tree-type structure based on their parentId.  This code works, but is obviously a little over-complicated. How can I optimize it?type ValidString = | ValidString of string | NotValidtype CategoryStructure = {    Id: ValidString;    ParentId: ValidString;    Name: ValidString;    Abbreviation: ValidString;    Description: ValidString;    SapId: ValidString;    Section: ValidString;    SectionPosition: ValidString}type DynamicCategories = {    Category: CategoryStructure;    SubCategories: seq<DynamicCategories>}let rec private structureCategories (fullList: CategoryStructure list)     (list: CategoryStructure list)  =    List.fold (fun acc elem ->                     // get all categories and details                    let categories = fullList                    let mainAcc =                        [                            for row in categories do                                if row = elem                                then                                    let subs =                                          List.fold (fun acc' elem' ->                                                    if row.Id = elem'.ParentStructureId                                                    then                                                        let modifiedList = elem' :: List.empty<CategoryStructure>                                                        let foundSubCategory =                                                             {                                                                Category = elem';                                                                SubCategories = structureCategories fullList modifiedList |> Seq.ofList                                                            }                                                        foundSubCategory :: acc'                                                    else acc'                                                    ) List.empty<DynamicCategories> categories                                        |> Seq.ofList                                            yield{                                        Category = elem;                                        SubCategories = subs                                    }                        ]                    mainAcc @ acc                    ) List.empty<DynamicCategories> list// get the initial parent categories and then call the above code to finish the organizing the listlet getStructuredCategories () =        let categories = allCategoriesAndDetails () |> List.ofSeq        [            for row in categories do                if row.ParentStructureId = NotValid                then yield row        ] |> structureCategories categories |> Seq.ofList
Creating a structured category list in F#
f#