Last login: Wed Mar 30 9:06:25 on ttys000 Marco-Bellias-MacBook-Air:~ marcob$ ocaml OCaml version 4.02.2 # (* anche tipi array e record, oltre le liste *);; # let single = [1];; val single : int list = [1] # let twoElem = 3::single;; val twoElem : int list = [3; 1] # hd twoElem;; Error: Unbound value hd # List.hd twoElem;; - : int = 3 # open List;; # hd(tl twoElem);; - : int = 1 # single@twoElem;; - : int list = [1; 3; 1] (* let-in: dichiarazioni locali e annidamento blocchi *) # let x = 5 in let prod x y = x*y in let rec fact = if n>0 then 1 else prod (fact (n-1)) n in fact x;; Error: Unbound value n # let x = 5 in let prod x y = x*y in let rec fact n = if n>0 then 1 else prod (fact (n-1)) n in# let rec fact = fun n -> if n==0 then 1 else n*fact(n-1);; val fact : int -> int = fact x;; - : int = 1 # let x = 5 and y = 17 and prod x y = x*y in let rec fact n = if n==0 then 1 else prod (fact (n-1)) n in y + fact x;; - : int = 120 # (* anche le funzioni sono valori *);; # let myfun = (fun x -> x+10, 7);; val pair : int -> int * int = # let pair = ((fun x -> x+10),7);; val pair : (int -> int) * int = (, 7) # let strange = fun (a,b) -> a b in strange pair;; - : int = 17 # (* tipi algebrici anche ricorsivi: alberi binari monomorfi *);; # type binTree = Empty |Leaf of int |Root of string * binTree * binTree ;; type tree = Empty | Leaf of int | Root of string * tree * tree # let t1 = Empty;; val t1 : binTree = Empty # let t0 = Empty;; val t0 : binTree = Empty # let leaf n = Leaf n;; val leaf : int -> binTree = # (* ed anche polimorfi *);; # type ('a,'b) bin = E |L of 'a |R of 'b * ('a,'b) bin * ('a,'b) bin ;; type ('a, 'b) bin = E | L of 'a | R of 'b * ('a, 'b) bin * ('a, 'b) bin # let t = R("A1", L 5, E);; val t : (int, string) bin = R ("A1", L 5, E) # t;; - : (int, string) bin = R ("A1", L 5, E)