let median l = let rec aux = function | e :: _, ([_] | [_; _]) -> e | _ :: tl1, _ :: _ :: tl2 -> aux (tl1, tl2) | _, _ -> invalid_arg "median" in aux (l, l)