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]