Ocaml Lang
Ocaml Lang
Syntax
Implementations are in .ml les, interfaces are in .mli les. Comments can be nested, between delimiters (*...*) Integers: 123, 1_000, 0x4533, 0o773, 0b1010101 Chars: a, \255, \xFF, \n Floats: 0.1, -1.234e-34
Functions
let f x = expr let rec f x = expr apply: let f x y = expr apply: let f (x,y) = expr Void, takes only one value: () Integer of either 31 or 63 bits, like 32 32 bits Integer, like 32l 64 bits Integer, like 32L Double precision oat, like 1.0 Boolean, takes two values: true or false Simple ASCII characters, like A Strings of chars, like "Hello" Lists, like head :: tail or [1;2;3] Arrays, like [|1;2;3|] Tuples, like (1,"foo", b) apply: List.iter (fun x -> e) l let f= function None -> act | Some x -> act apply: let f ~str ~len = expr apply: apply (for ~str:str): let f ?len ~str = expr let f ?(len=0) ~str = expr apply (with omitted arg): apply (with commuting): apply (len: int option): apply (explicitely ommited): let f (x : int) = expr let f : a b. a*b -> a = fun (x,y) -> x function with one arg recursive function f x with two args f x y with a pair as arg f (x,y) anonymous function function denition by cases f (Some x) with labeled args f ~str:s ~len:10 f ~str ~len with optional arg (option) optional arg default f ~str:s f ~str:s ~len:12 f ?len ~str:s f ?len:None ~str:s arg has constrainted type function with constrainted polymorphic type module denition module and signature module renaming include items from signature denition signature of module local module to 1st -class module from 1st -class module functor functor application open, include,
Conditionals
Structural Physical = == Polymorphic Equality <> != Polymorphic Inequality Polymorphic Generic Comparison Function: compare x<y x=y x>y compare x y -1 0 1 Other Polymorphic Comparisons : >, >=, <, <=
Data Types
unit int int32 int64 float bool char string a list a array t1 * ... * tn
Loops
while cond do ... done; for var = min_value to max_value do ... done; for var = max_value downto min_value do ... done;
Exceptions
exception MyExn exception MyExn of t * t exception MyFail = Failure raise MyExn raise (MyExn (args)) try expression with Myn -> ... new exception same with arguments rename exception with args raise an exception raise with args catch MyException if raised in expression virtual class with arg init before object creation object with self reference mutable instance variable accessor mutator virtual method init after object creation
Constructed Types
type record = { eld1 : bool; mutable eld2 : int; } type enum = | Constant | Param of string | Pair of string * int new record type immutable eld mutable eld new variant type Constant constructor Constructor with arg Constructor with args
Modules
module M = struct .. end module M: sig .. end= struct .. end module M = Unix include M module type Sg = sig .. end module type Sg = module type of M let module M = struct .. end in .. let m = (module M : Sg) module M = (val m : Sg) module Make(S: Sg) = struct .. end module M = Make(M) Module type items: val, external, type, exception, module, class
Constructed Values
let r = { field1 = true; field2 = 3; } let r = { r with field1 = false } r.field2 <- r.field2 + 1; let c = Constant let c = Param "foo" let c = Pair ("bar",3)
Pattern-matching
match expr with | pattern -> action | pattern when guard -> | _ -> action Patterns: | Pair (x,y) -> | { field = 3; _ } -> | head :: tail -> | [1;2;x] -> | (Some x) as y -> | (1,x) | (x,0) -> action conditional case default case
non-virtual class class variable constructor argument inheritance and ancestor reference method explicitely overriden access to ancestor copy with change new object method invocation immediate object closed variant union of variants argument must be a subtype of t t is a subtype of the argument
Imports Namespaces
open Unix;; let open Unix in expr Unix.(expr ) global open local open local open
variant pattern record pattern list pattern list-pattern with extra binding or-pattern
Polymorphic variants
type t = [ A | B of type u = [ A | C of type v = [ t | u | ] let f : [< t ] -> int | A -> 0 | B n -> let f : [> t ] -> int | A -> 0 | B n -> int ] float ] = function n = function n | _ -> 1