let rle xs =
  let rec aux xs cur n acc =
    match xs with
    | [] -> List.rev ((cur, n) :: acc)
    | h :: t ->
      if h = cur then aux t cur (n + 1) acc
      else aux t h 1 ((cur, n) :: acc)
  in
  match xs with
  | [] -> []
  | h :: t -> aux t h 1 []

;;

List.fold_left (fun acc (_, n) -> acc + n) 0
  (rle [1;1;1;2;2;3;3;3;3;1;1])
