OCaml for the Skeptical
U of C Library DLDC Informal OCaml Class

Built-In Operators and Functions

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!

Comparisons (Relational Operators)

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.

Integer Operators

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.

Floating-Point Operators

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.

Bitwise Operators

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!

String 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 expr1 has type string and expr2 has type int, e.g. "abc".[1] = 'b'. The String module contains many more functions, and the Str module contains regular expression operations (but remember the superior third-party PCRE library).

Boolean Operators

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

Pair Operators

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).

List Operators

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.

Array Operators

Arrays have a special syntax for accessing an element by index (i.e. subscripting): expr1.(expr2), where expr1 has type 'a array and expr2 has type int, e.g. Sys.argv.(0) = "/usr/local/bin/ocaml". Notice that you can tell string-subscripting from array-subscripting syntactically: the former uses brackets (string.[int]) while the latter uses parens (array.(int)).

Sequencing operators

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.

Unit operators

The function ignore takes one argument of any type and returns (). ignore expr is the same as expr; (), but the latter will generate a warning if expr is not of type unit, while ignore will suppress the warning.

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

format Operators

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

Reference Operators

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).

Conversion Operators

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 dtype is the type of the domain (i.e., the type to convert from) and rtype is the type of the range (i.e. the type of to convert to, the result). These include conversions to and from string for most other base types, e.g. string_of_bool, bool_of_string, int_of_string, string_of_int, etc.