This shows a little bit of OCaml. There is the OCaml REPL (Read, Evaluate, Print Loop), commonly launched from a command line with “ocaml”, which we demonstrate here. A command can be on several lines and ends with two semicolons. As in many languages many syntactic constructs, called expressions, return values. OCaml also reports the type of the value. In this note
3 => int = 3
means that when I type “3;;” and CR, the REPL prints “- : int = 3” on the next line.
5.3 => float = 5.3
1/3 => int = 0
“1./3;;” returns “This expression has type float but is here used with type int”. It has also underlined the “1.” of the input indicating what it is concerned with. The operator “/” is only for integers. “/.” is necessary for division of floats.
1. /. 3. => float = 0.333333333333333315
Ocaml has no coercion but concomitant compensating virtues gradually emerge. Next we cause “x” to refer to 3 and then examine its value:
let x = 3 in x => int = 3
It goes here without saying that anywhere we write a numeral, an expression could go and anywhere we write an applied occurrence of an identifier, we could write an expression. Thus:
let q = 1. /. 5. in q +. 3. => float = 3.2
“q” is bound above only for the stuff that follows the “in”. Of course that could be a big program but since we ended that stuff with “;;” q is now gone and now “q;;” yields “Unbound value q”. “let n = 6;;” binds 6 to “n” in the top level scope and may be used as a free variable in subsequent commands.

OCaml has a rich type system. Tuples are written as expressions separated with commas.
3, 5 => int * int = (3, 5)
The name of the type of a tuple is the names of the types of the parts, separated with asterisks.
"foo", 3.4, 5 => string * float * int = ("foo", 3.4, 5)
A list is written as expression of the same type, separated with semicolons and bounded with square brackets.
[3; 6; 1] => int list = [3; 6; 1]
An empty list is OK.
[] => 'a list = []
Note that OCaml doesn't know the type of the elements of this list yet. You can’t mix types in a list:
“[3; "not a numeral"];;” yields “This expression has type string but is here used with type int”.
Like Lisp or Scheme, these lists are composed and analyzed into the head and tail thus head::tail. The Head is the first element of the list and the tail is all the rest.
3::[] => int list = [3]
‘All the rest’ (the tail) is empty in this case and the head it 3. OCaml has deduced the type of the elements of the list now.
3::[4; 5] => int list = [3; 4; 5]

Matching

Pulling a list apart is best done with the “match” construct. A list is either empty, [], or has some elements, [3; ...] which is the same as 3::[...]. We name a value to test:
let m = [];;
val m : 'a list = []
Note that the side effect of binding “m” to [] caused OCaml to mention “m” and signal that it had been bound in the top scope. If we don’t know whether m is empty, we can say:
match m with [] -> "none" | a::b -> "more" => string = "none"
After “let m = [3; 6];;”
match m with [] -> "none" | a::b -> "more" => string = "more"
The vertical bar separates alternatives. OCaml first convinces itself that the alternatives are exhaustive, and then arranges that one of the expressions after a “->” will be evaluated to provide the value of the whole expression. The construct before the “->” is called a ‘pattern’ and typically includes some new identifiers. If the value being considered matches a pattern, then these identifiers are bound to the corresponding value parts, for purposes of evaluating the corresponding expression after the “->”.
match 3, 6 with s, d -> d => int = 6
match 3. , 5 with s, d -> s => float = 3.

Functions

let f x = x + 3;;
defines a function of one variable in the outer scope.
f 8 => int = 11
OCaml functions always take exactly one argument. Haskell Curry got to them. The notation might fool you and is such that some programs can be understood without knowing that functions take just one argument.
let f a b = a + b in f 3 4 => int = 7
suggests that f takes two arguments but behold:
let f a b = a + b in (f 3) 4 => int = 7
The value of “(f 3)” is a function of one integer argument which is applied to 4 in this sample.
let f a b = a + b in let g = f 2 in g 3 => int = 5
let rec fac n = match n with 0 -> 1 | k -> k * (fac (n - 1));;
val fac : int -> int = <fun>
fac 4 => int = 24
The “rec” makes the defined thing, fac, available for use in its own definition. Note also that the alternatives are considered in written order. “k” could have been bound to 0 except that the 0 pattern came first.
fac 12 => int = 479001600
fac 13 => int = -215430144
Note that int arithmetic is modulo 232. Here is a function that triples every element of a list of integers:
let rec tr ls = match ls with [] -> [] | a::b -> 3*a::(tr b);;
tr [2; 4; 5] => int list = [6; 12; 15]
A Scheme or Lisp programmer will find this familiar.