Int & Float

  • Operators on ints+ - * / mod (* modulo *)
  • Operators on floats+. -. *. /. mod_float

Booleans, IF

  • true false
  • Combinators : && ||
  • if whatever then result1 else result2
  • if with unit type:let () = if whatever then Printf.printf "Message 1." (* this empty else could be removed *) else () ; Printf.printf "Message 2" ; ()

Printf, string

  • Concat strings : s1 ^ s2
  • Read a character: s.[3] (* Returns the 4th character in s *)
  • Length of a string : String.length s
  • Printf: Printf.printf "An int : %d \n%!" x ; Printf.printf "A float : %f \n%!" x ; Printf.printf "A bool : %b \n%!" x ; Printf.printf "A string : %s \n%!" x ;
  • Do not forget line return \n and flush %!

Tuples

  • Create a tuple let tup = (value1, value2, value3)
  • Read a tuple let (a,b,c) = tup

Records

  • A record type with type parameter 'a :type 'a foo = { aa : int ; bb : 'a ; }
  • Create a record let r1 = { aa = 10 ; bb = "ok" }
  • Create a record from another record let r2 = { r1 with aa = 20 }
  • Read a record let x = r1.aa + r2.aa
  • Match a record match r2 with | { aa = x ; bb = "ok" } -> ... | { aa = x ; bb = _ } -> ...

A recursive record as a tree

  • type 'a tree = { node: 'a ; next: 'a tree list ; }
  • Count the number of nodes: let rec cnt tree = 1 + List.fold_left (fun acu n -> acu + cnt n) 0 tree.next

Arrays

  • An array : let ar = [| 1 ; 2 ; 3 ; 4 |]
  • or : Array.make 10 0 (* An array with 10 cells containing 0 *)
  • Read a cell : ar.(0) (* Reads the first cell *)
  • Write into a cell : ar.(0) <- 50 (* Returns unit *)
  • Array size : Array.length ar

Lists

  • Build a list : let la = [ 1 ; 2 ; 3 ; 4 ; 5 ]
  • equivalent to : let la = 1 :: 2 :: 3 :: 4 :: 5 :: []
  • Build a new list from an existing list : let lb = 20 :: 30 :: la
  • Read a list, with pattern matching : match lb with | [] -> ... | x :: rest -> ...
  • Match several cells with : | x :: y :: z :: rest -> ...

Usual functions

  • List.iter : ('a -> unit) -> 'a list -> unit : applies the given function to every list element.
  • List.rev : 'a list -> 'a list : reverses the list.
  • List.filter : ('a -> bool) -> 'a list -> 'a list : keeps only the list elements that satisfy the given predicate.
  • List.find_opt : ('a -> bool) -> 'a list -> 'a option : finds a list element that satisfies the given predicate.
  • List.map : ('a -> 'b) -> 'a list -> 'b list : applies a function to every list element, returns a new list.
  • List.filter_map : ('a -> 'b option) -> 'a list -> 'b list : map and filter combined.
  • List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a : fold (aka reduce)
  • How to write map : let rec map f = function | [] -> [] | x :: rest -> f x :: map f rest
  • How to write rev, with an acu : let rec rev acu = function | [] -> acu | x :: rest -> rev (x :: acu) rest

Algebraic datatypes

  • type 'a where = Nowhere | Here of int | There of 'a
  • Create values : let x = Nowhere let y = Here 20 let z = There [ true ; false ] (* z has type (bool list) where *)
  • Read values, by pattern matching : match y with | Nowhere -> ... | Here a -> ... | There b -> ...

Option type

  • This option type is predefined: type 'a option = None | Some of 'a

A recursive datatype as a tree

  • type 'a tree = Leaf of 'a | Node of ('a tree) * ('a tree)
  • Count the number of leaves: let rec cnt = function | Leaf _ -> 1 | Node (left, right) -> cnt left + cnt right

Functions: fun, function, match

  • All these definitions are equivalent :
  • let f x l = match l with | [] -> x | y :: _ -> x * y
  • let f = fun x l -> match l with | [] -> x | y :: _ -> x * y
  • let f x = function | [] -> x | y :: _ -> x * y

'as' in pattern matching:

  • match l with | [] -> ... | x :: ((y :: rest) as pp) -> ... (* pp contains y :: rest) *)

Exceptions

  • Predefined exceptions : exception Not_found exception Failure of string exception Exit exception Invalid_argument of string
  • Raise an exception : raise Not_found raise (Failure "message") (* equivalent to failwith "message" *)
  • Catch an exception : begin try (* (small or big code) *) with | Not_found -> ... | Failure x -> ... end begin end is optional, but recommended to avoid syntactic confusion, for example inside a pattern matching.