utop[1]> List.length;; - : 'a list -> int = utop[2]> List.length [];; - : int = 0 utop[3]> List.length [1;2;3];; - : int = 3 utop[4]> List.mem 4 [1;3;5;7];; - : bool = false utop[5]> List.mem 3 [1;3;5;7];; - : bool = true utop[6]> List.sort;; - : ('a -> 'a -> int) -> 'a list -> 'a list = utop[7]> List.sort (fun x y -> if x > y then 1 else if x = y then 0 else -1) [1;3;2;4;5;7];; - : int list = [1; 2; 3; 4; 5; 7] utop[8]> List.sort (fun x y -> if x < y then 1 else if x = y then 0 else -1) [1;3;2;4;5;7];; - : int list = [7; 5; 4; 3; 2; 1] utop[9]> List.find;; - : ('a -> bool) -> 'a list -> 'a = utop[10]> List.find (fun x -> x mod 2 = 0) [1;2;3;4;5];; - : int = 2 utop[11]> List.find_all (fun x -> x mod 2 = 0) [1;2;3;4;5];; - : int list = [2; 4] utop[12]> List.init;; - : int -> (int -> 'a) -> 'a list = utop[13]> List.init 10 succ 5;; Error: This function has type int -> (int -> 'a) -> 'a list It is applied to too many arguments; maybe you forgot a `;'. utop[14]> List.init 0 succ 5;; Error: This function has type int -> (int -> 'a) -> 'a list It is applied to too many arguments; maybe you forgot a `;'. utop[15]> List.init 0;; - : (int -> '_weak1) -> '_weak1 list = utop[16]> List.init 0 succ ;; - : int list = [] utop[17]> List.init 0 succ;; - : int list = [] utop[18]> List.init;; - : int -> (int -> 'a) -> 'a list = utop[19]> List.init 5 succ;; - : int list = [1; 2; 3; 4; 5] utop[20]> List.init 5 (fun i -> i + 2);; - : int list = [2; 3; 4; 5; 6] utop[21]> [] : int;; Error: Syntax error utop[22]> ([] : int);; Error: This expression has type 'a list but an expression was expected of type int utop[23]> (::) ;; Error: The constructor :: expects 2 argument(s), but is applied here to 0 argument(s) utop[24]> (+);; - : int -> int -> int = utop[25]> List.sort (fun x y -> if x < y then 1 else if x = y then 0 else -1) [1;3;2;4;5;7];; - : int list = [7; 5; 4; 3; 2; 1] utop[26]> List.sort (fun x y -> if x > y then 1 else if x = y then 0 else -1) [1;3;2;4;5;7];; - : int list = [1; 2; 3; 4; 5; 7] utop[27]> [];; - : 'a list = [] utop[28]> 1 :: [];; - : int list = [1] utop[29]> let l = 1 :: 2 :: 3 :: [];; val l : int list = [1; 2; 3] utop[30]> l;; - : int list = [1; 2; 3] utop[31]> List.hd l;; - : int = 1 utop[32]> List.tl l;; - : int list = [2; 3] utop[33]> 0 :: l;; - : int list = [0; 1; 2; 3] utop[34]> 0 :: l;; - : int list = [0; 1; 2; 3] utop[35]> let l = 0 :: l;; val l : int list = [0; 1; 2; 3] utop[36]> l;; - : int list = [0; 1; 2; 3] utop[37]> let rec length l = match l with [] -> 0 | _ :: tl -> 1 + length tl ;; val length : 'a list -> int = utop[38]> length l;; - : int = 4 utop[39]> let rec make_list e k = if k = 0 then [] else e :: make_list e (pred k) ;; val make_list : 'a -> int -> 'a list = utop[40]> make_list 3 10;; - : int list = [3; 3; 3; 3; 3; 3; 3; 3; 3; 3] utop[41]> List.init;; - : int -> (int -> 'a) -> 'a list = utop[42]> List.init 10 (fun x - x);; Error: Syntax error: operator expected. utop[43]> List.init 10 (fun x -> x);; - : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] utop[44]> List.init 10 pred;; - : int list = [-1; 0; 1; 2; 3; 4; 5; 6; 7; 8] utop[45]> l;; - : int list = [0; 1; 2; 3] utop[46]> 3 * 3 :: l;; - : int list = [9; 0; 1; 2; 3] utop[47]> l;; - : int list = [0; 1; 2; 3] utop[48]> let l2 = List.init 5 succ;; val l2 : int list = [1; 2; 3; 4; 5] utop[49]> l;; - : int list = [0; 1; 2; 3] utop[50]> let l1 = make_list 4 3;; val l1 : int list = [4; 4; 4] utop[51]> List.append l1 l2;; - : int list = [4; 4; 4; 1; 2; 3; 4; 5] utop[52]> List.append l2 l1;; - : int list = [1; 2; 3; 4; 5; 4; 4; 4] utop[53]> l1;; - : int list = [4; 4; 4] utop[54]> l2;; - : int list = [1; 2; 3; 4; 5] utop[55]> l2 @ l1;; - : int list = [1; 2; 3; 4; 5; 4; 4; 4] utop[56]> l2;; - : int list = [1; 2; 3; 4; 5] utop[57]> l1;; - : int list = [4; 4; 4] utop[58]> l;; - : int list = [0; 1; 2; 3] utop[59]> List.rev l;; - : int list = [3; 2; 1; 0] utop[60]> List.length;; - : 'a list -> int = utop[61]> List.length l;; - : int = 4 utop[62]> List.mem;; - : 'a -> 'a list -> bool = utop[63]> List.mem 10 l;; - : bool = false utop[64]> List.mem 1 l;; - : bool = true utop[65]> List.append;; - : 'a list -> 'a list -> 'a list = utop[66]> [1,2] @ [1.0];; Error: This expression has type float but an expression was expected of type int * int utop[67]> List.rev;; - : 'a list -> 'a list = utop[68]> List.sort;; - : ('a -> 'a -> int) -> 'a list -> 'a list = utop[69]> let l3 = l2 @ l1;; val l3 : int list = [1; 2; 3; 4; 5; 4; 4; 4] utop[70]> let l3 = l2 @ l1 @ l2;; val l3 : int list = [1; 2; 3; 4; 5; 4; 4; 4; 1; 2; 3; 4; 5] utop[71]> List.sort (fun x y -> if x > y then 1 else if x < y then -1 else 0) l3;; - : int list = [1; 1; 2; 2; 3; 3; 4; 4; 4; 4; 4; 5; 5] utop[72]> List.sort (fun x y -> if x > y then -1 else if x < y then 1 else 0) l3;; - : int list = [5; 5; 4; 4; 4; 4; 4; 3; 3; 2; 2; 1; 1] utop[73]> List.filter;; - : ('a -> bool) -> 'a list -> 'a list = utop[74]> l2;; - : int list = [1; 2; 3; 4; 5] utop[75]> List.filter (fun x -> x mod 2 = 0) l2;; - : int list = [2; 4] utop[76]> List.filter (fun x -> x mod 2 != 0) l2;; - : int list = [1; 3; 5] utop[77]> List.map;; - : ('a -> 'b) -> 'a list -> 'b list = utop[78]> List.map int_to_float l3;; Error: Unbound value int_to_float Hint: Did you mean int_of_float? utop[79]> List.map float_of_int l3;; - : float list = [1.; 2.; 3.; 4.; 5.; 4.; 4.; 4.; 1.; 2.; 3.; 4.; 5.] utop[80]> l3;; - : int list = [1; 2; 3; 4; 5; 4; 4; 4; 1; 2; 3; 4; 5] utop[81]> List.map (fun x -> x * x) l3;; - : int list = [1; 4; 9; 16; 25; 16; 16; 16; 1; 4; 9; 16; 25] utop[82]> List.find;; - : ('a -> bool) -> 'a list -> 'a = utop[83]> l2;; - : int list = [1; 2; 3; 4; 5] utop[84]> List.find (fun x -> x mod 2 = 0) l2;; - : int = 2 utop[85]> List.find (fun x -> x > 10) l2;; Exception: Not_found. utop[86]> List.find_all;; - : ('a -> bool) -> 'a list -> 'a list = utop[87]> List.find_all (fun x -> x mod 2 = 0) l2;; - : int list = [2; 4] utop[88]> List.find_all (fun x -> x > 10) l2;; - : int list = [] utop[89]> List.split;; - : ('a * 'b) list -> 'a list * 'b list = utop[90]> List.split [(1, "fewf"); (4; "gerg"); (4 , "ggg")] ;; Error: This expression has type string but an expression was expected of type int * string utop[91]> List.split [(1, "fewf"); (4, "gerg"); (4 , "ggg")] ;; - : int list * string list = ([1; 4; 4], ["fewf"; "gerg"; "ggg"]) utop[92]> [(1, "fewf"); (4, "gerg"); (4 , "ggg")] ;; - : (int * string) list = [(1, "fewf"); (4, "gerg"); (4, "ggg")] utop[93]> None;; - : 'a option = None utop[94]> Some 10;; - : int option = Some 10 utop[95]> List.find (fun x -> x > 10) l2;; Exception: Not_found. utop[96]> List.find;; - : ('a -> bool) -> 'a list -> 'a = utop[97]> let rec find_if pred l = match l with [] -> None | e :: t -> if pred e then Some e else find_if pred t;; val find_if : ('a -> bool) -> 'a list -> 'a option = utop[98]> List.find;; - : ('a -> bool) -> 'a list -> 'a = utop[99]> find (fun x -> x > 10) l2;; Error: Unbound value find utop[100]> find_if (fun x -> x > 10) l2;; - : int option = None utop[101]> find_if (fun x -> x mod 2 = 0) l2;; - : int option = Some 2 utop[102]> Sys.time;; - : unit -> float = utop[103]> Sys.time ();; - : float = 19.9672210000000021 utop[104]> Sys.time ();; - : float = 19.975995 utop[105]> let time f = let start = Sys.time () in let _ = f () in Sys.time () -. start ;; val time : (unit -> 'a) -> float = utop[106]> time;;; - : (unit -> 'a) -> float = utop[107]> let time f = let start = Sys.time () in let _ = f () in Sys.time () -. start ;; val time : (unit -> 'a) -> float = utop[108]> iota 10;; Error: Unbound value iota utop[109]> let iota n = List.init n (fun x -> x);; val iota : int -> int list = utop[110]> iota 10;; - : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] utop[111]> time (fun () -> List.mem 100 (iota 100));; - : float = 8.00000000111822374e-06