mp2i-info/tp4/bin/exo5.ml
2025-11-07 11:51:11 +01:00

19 lines
503 B
OCaml

let rec is_ordered l = match l with
| a::b::q -> a <= b && is_ordered (b::q)
| _ -> true;;
let rec is_rev_ordered l = match l with
| a::b::q -> a >= b && is_ordered (b::q)
| _ -> true;;
let rec has_ordering l = match l with
| a::b::q -> if a < b then
is_ordered (b::q)
else if a > b then
is_rev_ordered (b::q)
else has_ordering (b::q)
| _ -> true;;
assert (has_ordering [1; 1; 2; 2; 3]);;
assert (has_ordering [3; 2; 1; 1]);;
assert (not (has_ordering [1; 1; 2; 1]));;