2022-06-15

Iterate a nested record/dictionary OCaml

Sorry for asking a ridiculous question but I am a total beginner in OCaml.

I have two types:

type other = A | B
type someType = {a:string ; b:string ; c:other ; d:someType array}

How could I iterate through a record and get all occurrences of a key?

Since I do not know how deep the structure goes, I cannot figure out how many times I have to loop through each array to get the key.

let test = 
  {a = "a"; b = "b"; c = B;
   d =
    [|{a = "aa"; b = "bb"; c = A;
       d =
        [|{a = "aaa"; b = "bbb"; c = A;
           d =
            [|{a = "aaaa"; b = "bbbb"; c = A; d = [||]};
              {a = "aaaaa"; b = "bbbbb"; c = B; d = [||]}|]}|]}|]}

I want to count all a keys and sort them by c key. In this example I have 5 a keys, 2 of type B and 3 of type A. I want to return them as int * int = (3, 2)



No comments:

Post a Comment