epicours/Code CAML.md

744 B

let rec insert_post x f = function 
	| [] -> Failwith "x cannot be inserted"
	| e::t -> if f e then e::x::t else y::insert_post x f t;;
val insert_post: 'a -> ('a -> bool) -> 'a list -> 'a list = <fun>

let rec pos_max lst = let x = 1 in match lst with
	| [] -> invalid_arg "post_max: empty list"
	| e::[] -> x
	| e1::e2::t -> 
		if e2 > e1 then
			begin
			x = x + 1; 
			e2::pos_max t;
			end;
		else
			e1::pos_max t;;

let pos_max lst = 
	if lst = [] then
		failwith "pos_max: empty list"
	else 
		let rec pmr mpos cpos = function
			| e::[] -> mpos
			| mval::e::t ->
				if e > mval then
					pmr cpos (cpos + 1) (e::t)
				else 
					pmr mval mpos (cpos + 1) mval::t
		in pmr 1 1;;
val pos_max lst = 'a list -> int = <fun>