OCaml Standard Library OCaml v. 3.12.0 — June 7, 2011 — Copyright c 2011 OCamlPro SAS — http://www.ocamlpro.com/

Standard Modules module List module Array

Basic Data Types let len = List.length l let t = Array.create len v let t = Array.init len (fun pos -> v_at_pos) Pervasives All basic functions List.iter (fun ele -> ... ) l; let v = t.(pos) String Functions on Strings let l’ = List.map(fun ele -> ... ) l t.(pos) <- v; Array Functions on Polymorphic Arrays let l’ = List.rev l1 let len = Array.length t List Functions on Polymorphic Lists let acc’ = List. left (fun acc ele -> ...) acc l let t’ = Array.sub t pos len Char Functions on Characters let acc’ = List.fold right (fun ele acc -> ...) l acc let t = Array.of_list list Int32 Functions on 32 bits Integers if List.mem ele l then ... let list = Array.to_list t Int64 Functions on 64 bits Integers if List.for all (fun ele -> ele >= 0) l then ... Array.iter (fun v -> ... ) t; Nativeint Functions on Native Integers if List.exists (fun ele -> ele < 0) l then ... let neg = List.find (fun x -> x < 0) ints Array.iteri (fun pos v -> ... ) t; Advanced Data Types let negs = List.find all (fun x -> x < 0) ints let t’ = Array.map (fun v -> ... ) t let t’ = Array.mapi (fun pos v -> ... ) t Buffer Automatically resizable strings let (negs,pos) = List.partition (fun x -> x < 0) ints let ele = List.nth 2 list let concat = Array.append prefix suffix Complex Complex Numbers Array. compare t; Digest MD5 Checksums let = List.hd list Hashtbl Polymorphic Hash Tables let = List.tl list let value = List.assoc key assocs Queue Polymorphic FIFO module Char Stack Polymorphic LIFO if List.mem assoc key assocs then ... Stream Polymorphic Streams let assocs = List.combine keys values let ascii_65 = Char.code ’A’ Map Dictionaries (functor) let (keys, values) = List. assocs let char_A = Char.chr 65 Set Sets (functor) let l’ = List.sort compare l let c’ = Char.lowercase c let l = List.append l1 l2 or l1 @ l2 let c’ = Char.uppercase c System let list = List.concat list_of_lists let s = Char.escaped c Arg Argument Parsing Functions using Physical Equality in List Filename Functions on Filenames Format Pretty-Printing module Buffer memq, assq, mem_assq Genlex Simple OCaml-Like Lexer let b = Buffer.create 10_000 Marshal Serialization Functions Non-tail Recursive Functions in List Printf.bprintf b "Hello %s\n" name Lexing Functions for ocamllex Buffer.add_string b s; Parsing Functions for ocamlyacc append, concat, @, map, fold_right, map2, fold_right2, Buffer.add_char b ’\n’; Printexc Generic Exception Printer remove_assoc, remove_assq, split, combine,merge let s = Buffer.contents s Random Random Number Generator Printf printf-like Functions module String Scanf scanf-like Functions module Digest Sys OS Low-level Functions let s = String.create len let md5sum = Digest.string str let s = String.make len char Tweaking let md5sum = Digest.substring str pos len let len = String.length s let md5sum = Digest.file filename Lazy Functions on Lazy Values let char = s.[pos] let md5sum = Digest.channel ic len Gc Garbage Collection Tuning s.[pos] <- char; let hexa = Digest.to_hex md5sum Weak Weak Pointers (GC) let concat = prefix ^ suffix let s’ = String.sub s pos len’ Popular Functions per Module let s = String.concat "," list_of_strings module Filename module Hashtbl let pos = String.index_from s pos char_to_find let pos = String.rindex_from s pos char_to_find if Filename.check_suffix name ".c" then ... let t = Hashtbl.create 117 String.blit src src_pos dst dst_pos len; let file = Filename.chop_suffix name ".c" Hashtbl.add t key value; let s’ = String.copy s let file = Filename. name let value = Hashtbl.find t key let s’ = String.uppercase s let dir = Filename. name Hashtbl.iter (fun key value -> ... ) t; let s’ = String.lowercase s let name = Filename.concat dir file let cond = Hashtbl.mem t key let s’ = String.escaped s if Filename.is_relative file then ... Hashtbl.remove t key; String.iter (fun c -> ...) s; let file = Filename.temp_file prefix suffix Hashtbl.clear t; if String.contains s char then ... let file = Filename.temp_file ~temp_dir:"." pref suf module Marshal module Gc module Map let string = Marshal.to_string v module Dict = Map.Make(String) Gc.compact (); [Marshal.No_sharing; Marshal.Closures] module Dict = Map.Make(struct Gc.major (); let _ = Marshal.to_buffer string at_pos max_len v [] type t = String.t let compare = String.compare end) Gc.set { Gc.get() with let (v : of_type) = Marshal.from_string string at_pos let empty = Dict.empty Gc.minor_heap_size = 1_000_000; if String.length s > at_pos + Marshal.header_size then let dict = Dict.add "x" value_x empty Gc.max_overhead = 1_000_000; (* no compaction *) let needed = Marshal.total_size s at_pos in ... if Dict.mem "x" dict then ... }; let value_x = Dict.find "x" dict let new_dict = Dict.remove "x" dict module Random Dict.iter (fun key value -> ..) dict; let new_dict = Dict.map (fun value_x -> ..) dict Random.self_init (); module Weak let nee_dict = Dict.mapi (fun key value -> ..) dict Random.init int_seed; let acc = Dict.fold (fun key value acc -> ..) dict acc let int_0_99 = Random.int 100 let t = Weak.create size if Dict.equal dict other_dict then ... let coin = Random.bool () Weak.set t pos (Some v); let float = Random.float 1_000. match Weak.get t pos with None -> ... module Set module S = Set.Make(String) module Printexc module S = Set.Make(struct module Arg let s = Printexc.to_string exn type t = String.t let compare = String.compare end) let s = Printexc.get_backtrace () let empty = S.empty Printexc.register_printer (function let arg_list = [ let set = S.add "x" empty MyExn s -> Some (Printf.sprintf ...) "-do", Arg.Unit (fun () -> ..), ": call with unit" if S.mem "x" set then ... | _ -> None); ; "-n", Arg.Int (fun int -> ..), " : call with int" let new_set = S.remove "x" set ; "-s", Arg.String (fun s -> ..), " : call/w string" S.iter (fun key -> ..) dict; ; "-", Arg.Set flag_ref, ": set ref" let union = S.union set1 set2 module Lazy ; "-no", Arg.Clear flag_ref, ": clear ref" ] let intersection = S.inter set1 set2 let arg_usage = "prog [args] anons: run prog with args" let difference = S.diff set1 set2 let lazy_v = lazy (f x) Arg.parse arg_list (fun anon -> .. ) arg_usage; let min = S.min_elt set let f_x = Lazy.force lazy_v Arg.usage arg_list arg_usage; let max = S.max_elt set