What are basic the data structures ?

Contact the author Pierre.Weis@inria.fr

Created in October 1995.

Caml provides the following data structures: numbers (integers or floating point numbers), booleans, characters, character strings, arrays, lists, references. In addition, some library modules define extra abstract data types on top of these basic ones (in particular sets and streams, see below).

If none of these predefined data structures is suitable to model your data, you have to define your own data structures, with a type definition.

Table of contents:

Numbers: integers and floating point numbers.

Integers and floating point numbers correspond to the basic arithmetic capabilities provided by the hardware. Hence these numbers are easy to use but have some limitations, either in accuracy or range. Caml also provides numbers that support exact arithmetic operations: the arbitrary precision arithmetic package implements rational numbers, whose size may be as large as several thousand decimal digits. This package is a bit harder to use, than built-in arithmetics.

Characters

Characters are the elements of the type char. They are enclosed by the ` symbol:

#`a`;;
- : char = `a`
#`\n`;;
- : char = `\n`
#`\\`;;
- : char = `\\`

Other operations:

Character strings

Character strings are the elements of the type string; these are ordered sequels of letters. The size of a string is definitively defined when the string is created. Elements of a string may be updated.

Arrays

Arrays or vectors are the elements of the type 'a vect; these are ordered sequels of elements of type 'a. The size of an array is definitively defined when the array is created. Elements of an array may be updated.

Lists

Lists are the elements of the type 'a list; these are ordered sequels of elements of type 'a. The size of a list is dynamically modified, since elements can be added if necessary. A list is a recursive data structure: it is either empty, and then it is [] (say it ``nil''), or it is not empty and then it is built with an ``head'' of list, put in front of the ``rest'' or queue of the list. A non empty list is built by the application of the list constructor :: (an infix operator read as ``conse''). A non empty list with head ``x'' and queue ``rest'' is thus x :: rest. Elements of a list cannot be updated.

Booleans

Booleans are the elements of the type bool; these are values of boolean logic. true and false. These are the results of tests.

#1 = 2;;
- : bool = false
#1 <= 2;;
- : bool = true

Booleans get infix operations ``or'' and ``and'' of mathematical logic (respectively denoted by || and &&), and the negation (function not).

#(1 = 2) || not (1 <= 2);;
- : bool = false
#1 = 2 || not 1 <= 2;;
- : bool = false

Other operations:

Nothing

``void'' is the only element of the type unit. It is denoted by () (say it ``void''), and means the dummy result and argument of procedures.

#print_string "Hello World!";;
Hello World!- : unit = ()
#print_newline();;

- : unit = ()

References

References are the elements of the type 'a ref; these are memory locations that contain a value of type 'a. They are used in imperative programming to implement accumulators. The element stored in the location may be modified, that is replaced by another value of the same type.

Pairs

Pairs of a value of type 'a, and of a value of type 'b are the elements of the Cartesian product of 'a and 'b, denoted as 'a * 'b in Caml.

Other data structures

Other data structures available are system dependent. For instance, the Caml Light system offers:


Caml home page Last modified: Wednesday, April 14, 2004
Copyright © 1995 - 2004, INRIA all rights reserved.

Contact the author Pierre.Weis@inria.fr