OCaml is a functional programming language with support for imperative features like mutable state and side effects. It uses .ml and .mli files for implementations and interfaces respectively. Comments are delimited by (* and *). The language supports basic types like integers, floats, booleans, characters and strings as well as constructed types like tuples, lists, arrays, records and variants. Functions are defined using let and can be recursive, anonymous or curried. Conditionals and pattern matching are used for control flow. Modules are used for structuring code and provide abstraction through signatures. Exceptions are used for error handling.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
28 views
The Ocaml Language: Syntax Functions Conditionals
OCaml is a functional programming language with support for imperative features like mutable state and side effects. It uses .ml and .mli files for implementations and interfaces respectively. Comments are delimited by (* and *). The language supports basic types like integers, floats, booleans, characters and strings as well as constructed types like tuples, lists, arrays, records and variants. Functions are defined using let and can be recursive, anonymous or curried. Conditionals and pattern matching are used for control flow. Modules are used for structuring code and provide abstraction through signatures. Exceptions are used for error handling.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
The OCaml Language OCaml v. 3.12.0 — January 10, 2017 — OCamlPro SAS (http://www.ocamlpro.
com/)
Syntax Functions Conditionals
let f x = expr function with one arg Structural Physical Implementations are in .ml files, interfaces are in .mli files. let rec f x = expr recursive function = == Polymorphic Equality Comments can be nested, between delimiters (*...*) apply: f x <> != Polymorphic Inequality Integers: 123, 1_000, 0x4533, 0o773, 0b1010101 let f x y = expr with two args Polymorphic Generic Comparison Function: compare Chars: ’a’, ’\255’, ’\xFF’, ’\n’ Floats: 0.1, -1.234e-34 apply: f x y x<y x=y x>y let f (x,y) = expr with a pair as arg compare x y -1 0 1 Data Types apply: f (x,y) Other Polymorphic Comparisons : >, >=, <, <= unit Void, takes only one value: () List.iter (fun x -> e) l anonymous function int Integer of either 31 or 63 bits, like 32 let f= function None -> act function definition Loops int32 32 bits Integer, like 32l | Some x -> act by cases while cond do ... done; int64 64 bits Integer, like 32L apply: f (Some x) for var = min_value to max_value do ... done; float Double precision float, like 1.0 let f ~str ~len = expr with labeled args for var = max_value downto min_value do ... done; bool Boolean, takes two values: true or false apply: f ~str:s ~len:10 char Simple ASCII characters, like ’A’ apply (for ~str:str): f ~str ~len Exceptions string Strings of chars, like "Hello" let f ?len ~str = expr with optional arg (option) exception MyExn new exception ’a list Lists, like head :: tail or [1;2;3] let f ?(len=0) ~str = expr optional arg default exception MyExn of t * t’ same with arguments ’a array Arrays, like [|1;2;3|] apply (with omitted arg): f ~str:s exception MyFail = Failure rename exception with args t1 * ... * tn Tuples, like (1,"foo", ’b’) apply (with commuting): f ~str:s ~len:12 raise MyExn raise an exception apply (len: int option): f ?len ~str:s raise (MyExn (args)) raise with args Constructed Types apply (explicitely ommited): f ?len:None ~str:s try expression with let f (x : int) = expr arg has constrainted type |MyExn -> ... catch MyExn if raised in ex- type record = new record type let f : ’a ’b. ’a*’b -> ’a function with constrainted pression { field1 : bool; immutable field = fun (x,y) -> x polymorphic type mutable field2 : int; } mutable field Polymorphic variants new variant type Modules type enum = type t = [ ‘A | ‘B of int ] closed variant module M = struct .. end module definition | Constant Constant constructor type u = [ ‘A | ‘C of float ] module M: sig .. end= struct .. end module and signature | Param of string Constructor with arg type v = [ t | u | ] union of variants module M = Unix module renaming | Pair of string * int Constructor with args let f : [< t ] -> int = function argument must be include M include items from | ‘A -> 0 | ‘B n -> n a subtype of t module type Sg = sig .. end signature definition Constructed Values module type Sg = module type of M signature of module let f : [> t ] -> int = function t is a subtype | ‘A -> 0 | ‘B n -> n | _ -> 1 of the argument let r = { field1 = true; field2 = 3; } let module M = struct .. end in .. local module let r’ = { r with field1 = false } let m = (module M : Sg) to 1st -class module r.field2 <- r.field2 + 1; module M = (val m : Sg) from 1st -class module let c = Constant module Make(S: Sg) = struct .. end functor let c’ = Param "foo" module M = Make(M’) functor application let c’’ = Pair ("bar",3) Module type items: val, external, type, exception, module, open, include, References, Strings and Arrays class
let x = ref 3 integer reference (mutable) Pattern-matching
x := 4 reference assignation match expr with print_int !x; reference access | pattern -> action s.[0] string char access | pattern when guard -> action conditional case s.[0] <- ’a’ string char modification | _ -> action default case t.(0) array element access | exception exn -> action try&match (≥4.02.0) t.(0) <- x array element modification Patterns: | Pair (x,y) -> variant pattern Imports — Namespaces | { field = 3; _ } -> record pattern | head :: tail -> list pattern open Unix;; global open | [1;2;x] -> list-pattern let open Unix in expr local open | (Some x) as y -> with extra binding Unix.(expr ) local open | (1,x) | (x,0) -> or-pattern