OCaml for the Skeptical

The manual for the `Pervasives`
module describes all these operators and functions (hereafter I will just say
*operators*), so this page just tries to give some guidance and list some of the
operators mostly without explanation, just by way of orientation. See the manual for
details. This is by no means a complete listing!

The comparison operators are *polymorphic* meaning they work on most built-in data
types (except where it doesn't make sense; e.g. you can't (usefully) compare two
functions for equality). Note however that due to the strongly-typed nature of OCaml,
you can't compare two values of different types; e.g. `1 = "foo"` is not `false`
but rather a type error. Most of these operators will look familiar: `=`,
`<>`, `<`, `<=`, `>`, `>=`. OCaml distinguishes
between *structural equality* and *physical equality* (essentially equality of the
address of an object). `=` is structural equality and `==` is physical equality.
Beware: `<>` is *structural not-equals* while `!=` is *physical
not-equals*. `compare` is the polymorphic lexical-comparison function, useful for
sorting and searching the built-in data types.

Most of the operators on `int`'s should also look familiar: `+`, `-`, `*`,
`/` (integer division rounds towards zero), `mod`, `abs`. Unary (prefix)
negation is written with `-` but can also be written `~-` to distinguish partial
application of negation from that of subtraction:

# (-);; - : int -> int -> int = <fun> # (~-);; - : int -> int = <fun> #

See Partial Application.

Due to static-typing, OCaml requires floating-point versions of the integer operators
that are spelled differently (otherwise, would `a+b` be an `int` or a `float`)?
The basic arithmetic ops are spelled like the integer versions but with a trailing dot:
`+.`, `-.`, `*.`, `/.`. There is also `**` (exponentiation),
`sqrt`, `exp`, `log`, `log10`, and all the expected trig and hyperbolic trig
functions.

The set of bitwise operations on integers include an arithmetic shift right `asr` and
a set of logical operators: `land`, `lor`, `lxor`, `lnot`, `lsl`, and
`lsr`. N.B.: all of the bitwise operators are *infix* operators!

There's one `string` operator in the `Pervasives` module: `^`, the string
concatenation operator, e.g. `"a"^"b" = "ab"`. Strings also have a special syntax
for accessing a character by its index (i.e. subscripting): ` expr1.[expr2]`,
where

Unary negation with `not` and short-circuiting *and* `&&` and *or* `||`.

Remember that the type of tuples is determined by the types of the elements and by the
number of elements, so *pairs* (2-tuples) are a distinct type. They are are the only
tuple type to come with any built-in operations, to wit, the selectors `fst` and
`snd` (they are polymorphic in the types of the elements).

The only list operator in the `Pervasives` module is `@`, list concatenation.
There are many more functions in the `List` module; `List.hd` and `List.tl` are
Lisp's `car` and `cdr`.

Arrays have a special syntax for accessing an element by index (i.e. subscripting):
` expr1.(expr2)`, where

The semicolon `;` is not actually syntactically an infix operator in OCaml, but I
like to think of it that way. It takes two expressions, evaluates them in order from
left to right, and returns the rightmost value; thus, it serves to sequence expressions
with side-effects. For this reason, OCaml generates a warning if the types of the
subexpressions are not `unit`:

# 1;2;; Warning: this expression should have type unit. - : int = 2 #

You can use to `ignore` function to eliminate this warning; see below.

The function `ignore` takes one argument of any type and returns `()`. `ignore
expr` is the same as

# 1+2; ();; Warning: this expression should have type unit. - : unit = () # ignore (1+2);; - : unit = () #

The infix `^^` operator concatentates two `format` strings. The `Printf`
module contains the functions for printing with formats.

The prefix operator `!` and the infix operator `:=` for dereferencing and update,
and the functions `ref` (instantiate new reference) and `incr` and `decr`
(increment and decrement the value in a `ref`).

There is a wealth of type-conversion functions in the `Pervasives` module. They all
have (somewhat unidiomatic, English-wise) names of the form ` rtype_of_dtype`,
where