let rec split lst =
  match lst with
  | [] -> ([], [])
  | [x] -> ([x], [])
  | x :: y :: rest ->
    let (a, b) = split rest in
    (x :: a, y :: b)

let rec merge xs ys =
  match xs with
  | [] -> ys
  | x :: xs' ->
    match ys with
    | [] -> xs
    | y :: ys' ->
      if x <= y then x :: merge xs' (y :: ys')
      else y :: merge (x :: xs') ys'

let rec sort lst =
  match lst with
  | [] -> []
  | [x] -> [x]
  | _ ->
    let (a, b) = split lst in
    merge (sort a) (sort b)
;;

List.fold_left (+) 0 (sort [3;1;4;1;5;9;2;6;5;3;5])
