utop[0]> type pair = P of int * int let pair_cmp pair = let P(x,y) = pair in if x > y then 1 else if x < y then -1 else 0 let pair_cmp pair = match pair with P(x,y) -> if x > y then 1 else if x < y then -1 else 0 let pair_cmp pair = match pair with P(x,y) when x > y -> 1 | P(x,y) when x < y -> -1 | _ -> 0 let pair_cmp = function P(x,y) when x > y -> 1 | P(x,y) when x < y -> -1 | _ -> 0 ;; type pair = P of int * int val pair_cmp : pair -> int = val pair_cmp : pair -> int = val pair_cmp : pair -> int = val pair_cmp : pair -> int = utop[1]> Sys.time;; - : unit -> float = utop[2]> Sys.time ();; - : float = 0.694102 utop[3]> Sys.time ();; - : float = 0.7358849999999999 utop[4]> let start = Sys.time ();; val start : float = 0.755187 utop[5]> Sys.time () -. start;; - : float = 0.028281 utop[6]> let time f = let start = Syts.time () in let _ = f () in Sys.time () -. start;; Error: Unbound module Syts Hint: Did you mean Sys? utop[7]> let time f = let start = Sys.time () in let _ = f () in Sys.time () -. start;; val time : (unit -> 'a) -> float = utop[8]> time;; - : (unit -> 'a) -> float = utop[9]> [1;2] @ [3;4];; - : int list = [1; 2; 3; 4] utop[10]> List.append [1;2] [3;4];; - : int list = [1; 2; 3; 4] utop[11]> ( @ ) [1;2] [3;4];; - : int list = [1; 2; 3; 4] utop[12]> let rec iota n = if n = 0 then [] else iota (pred n) @ pred n;; Error: This expression has type int but an expression was expected of type 'a list utop[13]> let rec iota n = if n = 0 then [] else iota (pred n) @ (pred n);; Error: This expression has type int but an expression was expected of type 'a list utop[14]> let rec iota n = if n = 0 then [] else (iota (pred n)) @ (pred n);; Error: This expression has type int but an expression was expected of type 'a list utop[15]> let rec iota n = if n = 0 then [] else iota (pred n) @ [pred n];; val iota : int -> int list = utop[16]> iota 10;; - : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] utop[17]> time (fun () -> iota 10);; - : float = 2.99999999997524469e-06 utop[18]> time (fun () -> iota 100);; - : float = 0.000143000000000004235 utop[19]> time (fun () -> List.mem 100 (iota 100));; - : float = 0.000106000000000161521 utop[20]> time (fun () -> List.mem 0 (iota 100));; - : float = 0.000159000000000020236 utop[21]> let l = iota 100;; val l : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77; 78; 79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96; 97; 98; 99] utop[22]> time (fun () -> List.mem 0 l);; - : float = 2.99999999997524469e-06 utop[23]> time (fun () -> List.mem 100 l);; - : float = 5.00000000003275602e-06 utop[24]> let rec iota_quadratique n = (* O(n^2) *) if n = 0 then [] else iota_quadratique (pred n) @ [pred n];; val iota_quadratique : int -> int list = utop[25]> let iota n = let rec aux i = if i = n then [] else i :: aux (succ i) in aux 0;; val iota : int -> int list = utop[26]> iota 10;; - : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] utop[27]> time (fun () -> iota 1000);; - : float = 3.10000000001142695e-05 utop[28]> time (fun () -> iota_quadratique 1000);; - : float = 0.00935700000000006 utop[29]> time (fun () -> iota 10000);; - : float = 0.000642000000000031434 utop[30]> time (fun () -> iota_quadratique 10000);; - : float = 0.850597999999999743 utop[31]> List.init;; - : int -> (int -> 'a) -> 'a list = utop[32]> List.init 10 (fun i -> i);; - : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] utop[33]> let iota_rt n = let rec aux n l = if n = 0 then l else aux (pred n) (n :: l) in aux n [];; val iota_rt : int -> int list = utop[34]> iota_rt 10;; - : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] utop[35]> let iota_rt n = let rec aux n l = if n = -1 then l else aux (pred n) (n :: l) in aux (pred n) [];; val iota_rt : int -> int list = utop[36]> iota_rt 10;; - : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] utop[37]> time (fun () -> iota_quadratique 10000);; - : float = 0.850779000000000618 utop[38]> time (fun () -> iota 10000);; - : float = 0.000169999999999781437 utop[39]> time (fun () -> iota_rt 10000);; - : float = 0.000126999999999988233 utop[40]> time (fun () -> List.init 10000 (fun i -> i)));; Error: Syntax error utop[41]> time (fun () -> List.init 10000 (fun i -> i));; - : float = 0.000328000000000550074 utop[42]> time (fun () -> iota 100000);; - : float = 0.00679100000000065762 utop[43]> time (fun () -> iota 1000000);; Stack overflow during evaluation (looping recursion?). utop[44]> time (fun () -> iota_rt 1000000);; - : float = 0.0492449999999999832 utop[45]> List.init 10 (let l = ref [] in (fun i ->;; Error: Syntax error utop[46]> let _ = List.init 10 (let l = ref [] in (fun i -> begin l := 0 :: l; !l; end));; Error: This expression has type int list ref but an expression was expected of type int list utop[47]> let _ = List.init 10 (let l = ref [] in (fun i -> begin l := 0 :: !l; !l; end));; - : int list list = [[0]; [0; 0]; [0; 0; 0]; [0; 0; 0; 0]; [0; 0; 0; 0; 0]; [0; 0; 0; 0; 0; 0]; [0; 0; 0; 0; 0; 0; 0]; [0; 0; 0; 0; 0; 0; 0; 0]; [0; 0; 0; 0; 0; 0; 0; 0; 0]; [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]] utop[48]> let rec filter pred l = match l with [] -> [] | e :: tl -> let r = filter pred tl in if pred e then e :: r else r;; val filter : ('a -> bool) -> 'a list -> 'a list = utop[49]> let l = iota 10;; val l : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] utop[50]> filter (fun n -> n % 2 = 0) l;; Error: Unbound value % utop[51]> filter (fun n -> n mod 2 = 0) l;; - : int list = [0; 2; 4; 6; 8] utop[52]> let filter_rt pred l = let rec aux l r = match l with [] -> r | e :: tl -> if pred e then aux tl (e :: r) else aux tl r in aux l [];; val filter_rt : ('a -> bool) -> 'a list -> 'a list = utop[53]> filter_rt (fun n -> n mod 2 = 0) l;; - : int list = [8; 6; 4; 2; 0] utop[54]> let filter_rt pred l = let rec aux l r = match l with [] -> r | e :: tl -> if pred e then aux tl (e :: r) else aux tl r in List.rev (aux l []);; val filter_rt : ('a -> bool) -> 'a list -> 'a list = utop[55]> filter_rt (fun n -> n mod 2 = 0) l;; - : int list = [0; 2; 4; 6; 8] utop[56]> pred 10;; - : int = 9 utop[57]> succ 10;; - : int = 11 utop[58]> filter;; - : ('a -> bool) -> 'a list -> 'a list = utop[59]> List.map (fun x -> x * x) l;; - : int list = [0; 1; 4; 9; 16; 25; 36; 49; 64; 81] utop[60]> List.iter;; - : ('a -> unit) -> 'a list -> unit = utop[61]> let filter pred l = let r = ref [] in List.iter (fun e -> if pred e then r := e :: !r) l;; val filter : ('a -> bool) -> 'a list -> unit = utop[62]> filter (fun n -> n mod 2 = 0) l;; - : unit = () utop[63]> let filter pred l = let r = ref [] in begin List.iter (fun e -> if pred e then r := e :: !r) l; !r; end;; val filter : ('a -> bool) -> 'a list -> 'a list = utop[64]> filter (fun n -> n mod 2 = 0) l;; - : int list = [8; 6; 4; 2; 0] utop[65]> let filter pred l = let r = ref [] in begin List.iter (fun e -> if pred e then r := e :: !r) l; List.rev !r; end;; val filter : ('a -> bool) -> 'a list -> 'a list = utop[66]> filter (fun n -> n mod 2 = 0) l;; - : int list = [0; 2; 4; 6; 8] utop[67]> List.rev;; - : 'a list -> 'a list = utop[68]> List.rev l;; - : int list = [9; 8; 7; 6; 5; 4; 3; 2; 1; 0] utop[69]> List.fold_left;; - : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = utop[70]> List.fold_right;; - : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b = utop[71]> let length_left l = List.fold_left (fun x y -> x + 1) 0 l;; val length_left : 'a list -> int = utop[72]> length_left l;; - : int = 10 utop[73]> let length_right l = List.fold_right (fun x y -> x + 1) l 0;; val length_right : int list -> int = utop[74]> length_right l;; - : int = 1 utop[75]> let length_right l = List.fold_right (fun x y -> y + 1) l 0;; val length_right : 'a list -> int = utop[76]> length_right l;; - : int = 10 utop[77]>