letIn OCaml calling a function f with arguments x and y is written: “f x y=expr1inexpr2

let f x y = x + 2 * y in f 3 5yields 13. The construct is highly parallel to Scheme’s

The notion of pairs, triples and general tuples pervades the syntax and semantics of OCaml.
“`x, y`” denotes a pair.

let x, y = 3, 6 in x+yyields 9 and

let p = 3, 6 in let x, y = p in x+ydoes too where p is the pair. The text between

let a = 3 and b = 4 in a+bgives 7.

let a = 3 and b = 4 in let a = b and b = a in a-bgives 1. The first identifier after a

`rec` is a keyword in OCaml too and

let rec f x = if x = 0 then 1 else x * (f (x - 1)) in f 8defines the factorial function and yields 40320. In this variation the function

Being a functional language OCaml has a lambda expression.
`(fun x -> 2*x)` is the function that doubles its integer argument.
Thus

(fun x -> 2*x) 4gives 8.

OCaml has infix operators as in “`3 + 5`”.
When the operator needs to be named as a function in other contexts it is written “`(+)`”.
“`(+) 3 5`” means the same as “`3 + 5`”.
New operators may be defined as component wise addition as in vectors.

let (+^) (a, b) (c, d) = a + c, b + d in (3, 6) +^ (4, 2)gives (7, 8). This code uses several such new infix operators for 2D vectors.

Unlike Haskell, OCaml has constructs with side effects.
Many of my programs avoid all side effects but sometimes that is awkward.
The construct “` expr1; expr2`” performs

let x = ref 0 in x := 4; !x

Arrays are, unfortunately, always mutable and some record (struct) fields are too.
If b is an array then `b.(3) <- 9` changes the forth entry to hold 9.

Rich libraries come standard with OCaml.
“`Array.init` is a function that comes out of the module concerned with arrays.
It makes an array given the size and a function whose yield is the initial valley for each element of the array.

Array.init 10 (fun j -> j*j)produces an array of the squares of integers from 0 to 9.

let b = Array.init 10 (fun j -> j*j) in for k=0 to 9 do Printf.printf "%d %2d\n" k b.(k) donecreates the table and prints it. This code also illustrates the for loops and printing ala C.

There are these sorts of multiple values in OCaml:

- tuples, which can collect different types into one value,
- lists and arrays each of which must be of a uniform type,
- records with named fields.

let rec pli lst = match lst with a::b -> (print_int a; pli b) | [] -> () in pli [3; 6; 4]One more construct above:

matchIf the value of expr0 matches pattern 1 then the value (and effects) of the whole expression are that of expr1. The new variables in the pattern, hereexpr0withpattern1->expr1|pattern2->expr2

In the program that inspired this not there is only a minor mention of types:

type tv = float * float;; type zn = {mutable p : tv; mutable v : tv};;introduces

To use some jargon OCaml has no ‘strong’ contexts.
Most language since fortran knew that x in sqrt(x) had to be a floating point number and so if it saw sqrt(3) it converted the 3 to floating point.
Thus the context between the parens in sqrt(3) is strong.
In OCaml you must write “`sqrt 3.`” or “`sqrt (float 3)`”.
All contexts are week in OCaml and the type of an expression is determined by the expression, not its context.
The +. operator is floating add. If you want to add one to floating x then you must write "x +. 1.".
The `+.` establishes two contexts on either side and the compiler deduces things about the expressions that appear there.
The text `x +. y` informs the compiler that x and y are both floating point.
This seems to be enough clue that OCaml programs spend little other hair telling the compiler about types.
Some think it is twice too clever but I am undecided.