mp2i-info/dm4/lib/lib.ml
2025-11-14 09:15:10 +01:00

47 lines
1.1 KiB
OCaml

let plusetfois a b = (a+b, a*b);;
let rec ppp2_h n = match n with
| 0 -> 1
| _ -> 2 * (ppp2_h (n/2))
let ppp2 n = if n == 0 then 0 else ppp2_h (n-1)
let leeloo l = match l with
| _::_::_::_::a::_ -> a
| _ -> failwith "Not enough elements"
let rec coupe l = match l with
| a::b::q ->
let qa, qb = coupe q in
a::qa, b::qb
| _ -> l, []
let rec colle la lb = match la with
| [] -> lb
| a::q -> a::(colle lb q)
let rec compte l x = match l with
| [] -> 0
| a::q -> if a == x then 1+(compte q x) else compte q x
let rec sommesi l f = match l with
| [] -> 0
| a::q -> if f a then a+(sommesi q f) else (sommesi q f)
let rec majopred_h l f acc acc_n = match l with
| [] -> 2*acc >= acc_n
| a::q -> majopred_h q f (if f a then acc+1 else acc) (acc_n+1)
let majopred l f = majopred_h l f 0 0
let rec lexico la lb = match la, lb with
| [], _ -> true
| _, [] -> false
| a::_, b::_ when a < b -> true
| a::_, b::_ when a > b -> false
| _::qa, _::qb -> lexico qa qb
let rec aplatir l = match l with
| [] -> []
| []::ql -> aplatir ql
| (a::q)::ql -> a::(aplatir (q::ql))