An Ocaml program is a sequence of expressions or declarations that are evaluated one after another.
Ocaml programs are compiled into code – either byte code or native code – that evaluates the expressions.
Class and labs use a “toplevel” REPL (read-eval-print-loop) – utop – to interactively evaluate expressions.
Utop history files and lecture code are posted in notes2041-f20 github repo.
Every expression belongs to a single type. If its evaluation terminates normally, the result is a value of that type.
unit: ()int: 0,1,-1,23,-4611686018427387904,…string: "","ohai world", "I CAN HAZ CAPS",…bool, char, floatOcaml has standard operations on these basic types:
int: +,-,*,/,…float: +., -., *., /., **Hickey ch. 2 lists more
Compiling a malformed expression will result in a syntax error:
(1+1
Compiling an expression that has the wrong type will result in a type error:
1 +. 3
This is different from a runtime error:
if b then 1/0 else 13
Many programs that cause runtime errors in e.g. python, Java, cause type errors in OCaml.
let expressionslet declarations bind a name to a value for the rest of a program:
let name = value
let expressions bind a name for use in an expression:
let name = value in expr
such expressions are valid expressions, so they can be nested:
The scope of a binding is the program text in which it can be used:
The result of the expression let name = value in expr is expr, so the type of the expression is the type of expr:
let y = 42 in "hello!"
let b = true in 6.02e23
let x = 5 in
let y = x*2 in y = 10
let a = 0.1 in
let b = 2 in a < b(Assuming name has the type of value)
if…then..elseThe expression if cond then then else else evaluates cond, and if it is true, evaluates then, otherwise else.
Since the expression must belong to a single type, both then and else must have the same type.
OCaml defines the comparisons <, >, <=, >=, and = between expressions of the same type.
OCaml also has functions. They are applied to an argument to produce a result:
string_of_float 3.14159265 applies
string_of_float to3.14159265 : float to compute the"3.14159265" : stringread_int () applies function read_int to argument () : unit to compute a result of type int.The type of a function has the form t1 -> t2, where t1 is the type of argument the function expects, and t2 is the type of result it returns, so
string_of_float : float -> stringread_int : unit -> intcs2041.org