Question: 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

```

Answer: 
As already noted, you can't use `.length` on lists in OCaml. Instead you can pass them to `List.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 List.length n > List.length l then n 
      else l

```

But if we try it out:

```
# longest [[1;2;3]; [4;5]];;
Exception: Failure "you entered an empty list".

```

This occurs because eventually your recursion results in calling `longest` on an empty list. This can be resolved if we add a case for only a single element.

If we look for the longest list in a list with only one list, obviously that one list is the longest, so we just return that list.

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

```

As an alternative to raising an exception, you could use an `option` type.

```
let rec longest (nss: 'a list list) : 'a list option =
   match nss with
   | [] -> None
   | n::ns ->
       (match longest ns with   
        | None -> Some n
        | Some l when List.length l > List.length n -> Some l
        | _ -> Some n)

```

Now:

```
# longest [[1;2;3]; [4;5]];;
- : int list option = Some [1; 2; 3]
# longest [];;
- : 'a list option = None

```

But your function is not tail-recursive. Use it with a large enough list and you'll get a stack overflow. We can overcome this by using a locally scoped auxiliary (`aux`) function with an accumulator argument.

```
let longest lst =

  let rec aux lst acc =
    match lst with   
    | [] -> acc
    | fst::rst when List.length fst > List.length acc -> aux rst fst
    | _::rst -> aux rst acc 
  in

  match lst with
  | [] -> None
  | n::ns -> Some (aux ns n)

```

The `List.fold_left` function generalizes this "iterate over a list with an accumulator" behavior.

```
let longest lst =

  let len = List.length in

  let maxlen list1 list2 =
    if len list2 > len list1 then list2
    else list1
  in

  match lst with
  | [] -> None
  | (x::xs) -> Some (List.fold_left maxlen x xs)

```

A very basic implementation of a `fold_left` function may be instructive.

```
let rec fold_left f init lst =
  match lst with
  | [] -> init
  | (x::xs) -> fold_left f (f init x) xs

```
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

```