#use "sections.ml";; (** CAML : Un langage fonctionnel *) (** Expressions **) section "expressions";; 1+2;; 4/(2*2);; fun x->x*x;; section "let";; (** Définitions : let *) let x = 2*5+1;; let pi = 3.1416;; let square = fun x ->x*x;; section "letin";; (** Définitions locales : let ... in ... *) let x= 2*5+1;; let x= 2 in x*x;; x;; let a = 1 and b = 2 in 2*a+b;; let a=1 in let b=2*a in b+a;; let a=1 and b=2*a in b+2+a;; (** Types élémentaires **) section "intfloat";; (** int et float *) 1;; 1.;; 1 + 2;; 1.1 + 2.2;; 1.1 +. 2.2;; 3.45;; 3.45 E 10;; 3.45E10;; 3.45e10;; int_of_float 1.;; int_of_float 1.1;; float_of_int 76;; (* #open float;; open float;; #open "float";; 1.1 + 2.2;; 1+2;; #close "float";; *) (** bool *) section "bool";; 1<2;; 1<=2;; 0<1 && 3<9;; 0<1 & 3<9;; true && false;; true || false;; true or false;; section "ifthenelse";; let x=3 in if x=0 then 0 else 7/x;; if true then 1 else 0.;; section "string";; (** string *) "ceci est une"^" chaine de caracteres";; "ceci est une" ^ "chaine de caracteres";; section "char";; (** char *) 'a';; "a";; `a`;; (** Cette syntaxe est valable pour camlligth, mais non pour ocaml *) int_of_char 'a';; char_of_int 97;; section "produitcartesien";; (** Produit cartésien *) 1,2,3;; 1,2;; 1, (2,3);; ("coucou" , 3.1, ('A',2));; (TRUE & FALSE, 3+3);; true & false, 3+3;; fst;; snd;; let x=(1,"coucou") and y=("hello",2.1) in (snd x, fst y);; (** Expressions fonctionnelles **) section "application";; (** Application d'une fonction à un élément *) (fun x -> x*x) 4;; (function x -> x*x) 4;; square 4;; section "application2";; square 2+3;; square (2+3);; section "osuperieur";; (** Ordre supérieur *) section "currifiee";; let cf = fun x -> fun y-> x*y;; section "ocurrifiee";; let cf = fun x y -> x*y;; let cf x = fun y -> x*y;; let cf x y = x*y;; section "cfa2";; cf 2 34;; section "triple";; let triple = cf 3;; triple 21;; section "fargument";; let comp f g x = f (g x);; comp square triple 4;; comp square;; let f = comp square int_of_float;; f 4.1;; section "etareduction";; (** Eta-réduction *) let triple = fun x -> cf 3 x;; (** Fonctions récursives **) section "letdejadefinit" let x =1;; let x = x+2;; let x = let x=1 in x+2;; let x = x+2 where x=1;; section "fact" let rec fact n = if n=0 then 1 else n*fact(n-1);; (fact 4);; section "factmaldefinit" let rec fact n= if n=0 then 1 else n*(fact n-1);; (fact 4);; section "autrefact" let rec f = (fun n-> (if n=0 then 1 else n*f(n-1)));; (f 4);; section "puissancec" let rec puiss n x= if n=0 then 1. else x*.(puiss (n-1) x);; puiss 2 3.;; (puiss 200000 1.0);; section "puissancee" let rec puiss n x = if n=0 then 1. else let p =(puiss (n/2) x) in if (n mod 2)=0 then p*.p else p*.p*.x;; puiss 10 2.;; puiss 2000000 1.0;; puiss 20000000000 1.;; section "puissancebad" let rec puiss_bad n x = if n=0 then 1. else if (n mod 2)=0 then (puiss_bad (n/2) x)*.(puiss_bad (n/2) x) else x*.(puiss_bad ((n-1)/2) x)*.(puiss_bad ((n-1)/2) x);; puiss_bad 20000000 1.;; (** Fonctions mutuellement récursives *) section "foncmutrec";; let rec even n = if n=0 then true else odd(n-1) and odd n = if n=0 then false else even(n-1);; section "foncmutrec2";; let rec even n = let odd n = if n=0 then false else even(n-1) in if n=0 then true else odd(n-1);;