(* Copyright (C) 2000-2001 Samuel Thibault This program is free software : you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation ; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the program ; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. This License should be available in the same directory as this program, in a file called COPYING. If not, you may ask the webmaster or ftpmaster *) (* tout est en CamL (attention, version 0.74 !)*) let rev s= let n=string_length s in let r=make_string n ` ` in for i=0 to n-1 do r.[i]<-s.[n-1-i] done; r;; let sqrt n= if n<0 then failwith "un nombre négatif n'a pas de racine carrée" else let m=ref 0 in while !m * !m<=n do incr m done; !m-1;; let maxdup s= if s="" then 0 else let n=string_length s in let lmax=ref 1 and m=ref 1 and c=ref s.[0] in for i=1 to n-1 do if !c=s.[i] then incr m else ( c:=s.[i]; lmax:=max !lmax !m; m:=1 ) done; !lmax;; exception Deux_gagnants;; let morpion m= let gagnant=ref 0 in let check n= if abs n=3 then if !gagnant = -n/3 then failwith "il y a deux gagnants !" else gagnant:=n/3 in for i=0 to 2 do check (m.(i).(0)+m.(i).(1)+m.(i).(2)) (* pas la peine de faire un for ! *) done; for j=0 to 2 do check (m.(0).(j)+m.(1).(j)+m.(2).(j)) done; check (m.(0).(0)+m.(1).(1)+m.(2).(2)); check (m.(2).(0)+m.(1).(1)+m.(0).(2)); !gagnant;; let devine_age()= print_string "Je vais deviner votre âge, veuillez répondre par O ou N\n"; let min=ref 0 and max=ref 130 in while !min < !max do let mid=(!min + !max) / 2 in if mid = !min then ( print_string "Avez-vous "; print_int !min; print_string " ans ? " ) else ( print_string "Avez-vous entre "; print_int !min; print_string " et "; print_int mid; print_string " ans (inclus) ? " ); match read_line() with "O"|"o"->max:=mid |"N"|"n"->min:=mid+1 |_->print_string "Veuillez ne répondre que par O ou N\n"; done; print_string "Vous avez "; print_int !min; print_string " ans.\n";; exception dont_match;; let une_regexp s= let n=string_length s in if s.[n-1]<>`b` then false else try let i=ref 0 in while !iincr i |`b`->if s.[!i+1]=`c` then i:=!i+2 else raise dont_match |_->raise dont_match done; true with dont_match->false;; let rec puissance x=function 0->1 |1->x |n->let r=puissance x (n/2) in if n mod 2=0 then r*r else r*r*x;; (* pour a matrice carrée de côté n, calculer la puissance n_ème canonique *) let complexité a= let n=vect_length a in for i=0 to n-1 do for j=0 to n-1 do a.(i).(j)<-puissance a.(i).(j) n done done; a;; let jeu t= let n=vect_length t in let totaux=make_matrix n n (-1) in let rec total_au_pire i j= (* une fois qu'on a joué *) if totaux.(i).(j) <> -1 then totaux.(i).(j) else let res=match j-i with 0->0 |1->min t.(i) t.(j) |_->if pire_gauche i j > pire_droite i j then max (pire_gauche (i+1) j) (* l'autre joueur prendra à gauche !*) (pire_droite (i+1) j) else max (pire_gauche i (j-1)) (pire_droite i (j-1)) in totaux.(i).(j)<-res ; res and pire_gauche i j=total_au_pire (i+1) j + t.(i) (* si on prend à gauche *) and pire_droite i j=total_au_pire i (j-1) + t.(j) (* si on prend à droite *) in if pire_gauche 0 (n-1) > pire_droite 0 (n-1) then "gauche" else "droite";; (* trouvé de la doc sur Haskell et sur Beta sur Internet, mais pas eu le temps de m'y mettre *)