(* string_of_list, taking string_of_(item_type) as an argument*) let string_of_list (item_type:'a->string) (xs:'a list) : string = let rec print_list (f) (xs) : string = match xs with | [] -> "]" | [hd] -> (f hd) ^ "]" | hd::tl -> (f hd) ^ ";" ^ (print_list f tl) in "[" ^ print_list item_type xs ;; (* string_of_option, taking string_of_(item_type) as an argument*) let string_of_option (item_type:'a->string) (x:'a option) : string = match x with | None -> "None" | Some v -> "Some " ^ (item_type v) ;; (* alias for string_of_{int,char,float,bool,string} for use with string_of_{list,option} to maintain English readability *) let of_int = Printf.sprintf "%d";; let of_char = Printf.sprintf "%c";; let of_float = Printf.sprintf "%f";; let of_bool = Printf.sprintf "%B";; let of_string = Printf.sprintf "%s";; (* aliases, ostensibly for use as parameter to maintain English-language readability as above*) let of_list = string_of_list let of_option = string_of_option (* examples *) let tester () = let int1list = [1;2;3] in let str_int1list = string_of_list of_int int1list in print_endline str_int1list ; let int2list = [[1;2;];[];[3]] in let str_int2list = string_of_list (of_list of_int) int2list in print_endline str_int2list ; let intoption3list = [[[Some 1];[Some 2]];[[Some 3;Some 4];[Some 5; None];[]]] in let str_intoption3list = string_of_list (of_list (of_list (of_option of_int))) intoption3list in print_endline str_intoption3list ; let f1 = 4.0 in let f2 = 5.0 in let sum = f1 +. f2 in print_float (f1 +. f2); let some_sum' = Some ((float_of_int (Random.int 10)) +. sum) in print_endline (of_option of_float some_sum')