ocaml

OCaml function need clarification


I am trying to write an OCaml function that takes a list of list and return the longest list of them and I keep getting that n.length and l.length is unbound record field length

let rec longest (nss: 'a list list) : 'a list =
   match nss with
   | [] -> raise (Failure "you entered an empty list")
   | n::ns -> 
       let l = (longest ns) in
       if n.length > l.length then n 
       else l

Solution

  • To expand on Chris's answer.. You'd probably use the List module's functionality in real life.

    let lol =
      [
        [1];
        [1; 2; 3; ];
        [1; 2; 3; 4; 5; ];
        [1; 2; 3; 4; ];
        [1; 2; ];
      ]
    
    let ans =
      List.fold_left
        (
          fun a e -> 
              (*your code goes here*)
        )
        None
        lol