Skip to content

Instantly share code, notes, and snippets.

@blackh
blackh / td4.ml
Created March 21, 2012 12:00
TD4
let rec list_of_string = fun
| "" -> []
| s -> let n = string_length s in let t = sub_string s 1 (n-1) in
s.[0]::( list_of_string t );;
list_of_string "abcd";;
let rec string_of_list = fun
| [] -> ""
| [x] -> string_of_char(x)
@blackh
blackh / colle4.ml
Created March 15, 2012 09:10
Colle 4
let rec est_luka_aux = fun
[x] s -> ( (s+x) = -1 )
|(h::q) s -> ( (s+h) >= 0 ) && (est_luka_aux q (s+h));;
let est_luka l = est_luka_aux l 0;;
est_luka [1;-1;1;-1;1;-1;-1] ;;
[1;-1;-1;1;-1;1;1;1;-1;-1]
@blackh
blackh / td3.ml
Created February 21, 2012 20:54
TD 3
let rec takewhile p l = match (l, p (hd l)) with
|([], _) -> []
|(_, false) -> []
|((h::q), true) -> h::(takewhile p q);;
takewhile (fun x -> x>0) [8;5;-7;2;0;1];;
let rec dropwhile p l = match (l, p (hd l)) with
|([], _) -> []
|(l, false) -> l
@blackh
blackh / td2.ml
Created February 6, 2012 22:07
TD 2 PUBLIC
let rec dicho v l = match (vect_length v) with
|1 -> (v.(0) = l)
|n -> if ((n mod 2) = 0) then (
if (v.(n/2) <= l) then (dicho (sub_vect v (n/2) (n/2)) l)
else (dicho (sub_vect v 0 (n/2)) l);)
else (if(v.(n-1) = l) then true else (dicho (sub_vect v 0 (n-1)) l));;
dicho [|2;3;4;5|] 5;;
(* (* (* (* (* *) *) *) *) *)
@blackh
blackh / colle3.ml
Created February 2, 2012 08:53
Colle 3
let rec suppri_list test l =
match (l, (if (l<>[]) then test (hd l) else false;)) with
| ([], _) -> []
| (_, true) -> suppri_list test (tl l)
| (_, false) -> (hd l)::(suppri_list test (tl l));;
suppri_list (modulo 2) (init 10);;
@blackh
blackh / colle2.ml
Created January 31, 2012 17:35
Colle 2 - Pliage
let rec retourner ch = match string_length ch with
0 -> ch
| n -> let sch = (sub_string ch 1 (n-1)) in (retourner sch)^(char_for_read(ch.[0]));;
retourner "abcd";;
let rec compl_aux ch ch2 = match string_length ch with
0 -> ch2
| n -> ( if (ch.[0] = `0`) then
compl_aux (sub_string ch 1 (n-1)) (ch2^"1")
else
@blackh
blackh / silv.ml
Created January 24, 2012 20:45
Silverman
let silv n =
let u = make_vect (2*n+1) 0 in
let k = ref 2 in let j = ref 3 in let p = ref 3 in
u.(1)<- 1; u.(2)<-2; u.(3)<-2;
while u.(n)==0 do
for i=1 to !k do u.(!p+i) <- !j; done;
p:=!p+(!k); j:=!j+1; k:=u.(!j);
done;
u;;
@blackh
blackh / td1p.ml
Created January 24, 2012 20:45
TD Public
let tri e =
let tmp = ref 0 in
if (e.(0)<e.(2)) then (tmp:=e.(0); e.(0)<-e.(2); e.(2) <- !tmp);
if (e.(1)<e.(0)) then (tmp:=e.(0); e.(0)<-e.(1); e.(1) <- !tmp);
if (e.(2)<e.(1)) then (tmp:=e.(1); e.(1)<-e.(2); e.(2) <- !tmp);
e;;
let echange i j v =
let tmp = ref 0 in
tmp:=v.(i); v.(i)<-v.(j); v.(j)<-(!tmp);