let is_monotonic xs =
  match xs with
  | [] -> true
  | [_] -> true
  | _ ->
    let inc = ref true in
    let dec = ref true in
    let rec walk prev rest =
      match rest with
      | [] -> ()
      | h :: t ->
        if h < prev then inc := false;
        if h > prev then dec := false;
        walk h t
    in
    (match xs with h :: t -> walk h t | [] -> ());
    !inc || !dec

;;

(if is_monotonic [1;2;3;4] then 1 else 0) +
(if is_monotonic [4;3;2;1] then 1 else 0) +
(if is_monotonic [1;2;1] then 1 else 0) +
(if is_monotonic [5;5;5] then 1 else 0) +
(if is_monotonic [] then 1 else 0)
