![]() | ![]() | ![]() | Exercise 4 - Closed intervals whose bounds are integers | Documentation and user's manual | Table of contents | OCaml programs |
The non-built-in type closed_interval
will be used.
It is provided by the Closed_interval
module.
Specification of the type closed_interval |
This type gives each closed interval whose bounds are integers a unique representation.
It is equipped with the five following functions:
Closed_interval.mli file) shows the types of these five functions:
closed_interval is abstract.
The type definition phrase type closed_interval only contains the keyword type followed by the name closed_interval , indeed.
After this there is nothing.
Therefore the type representation is hidden.
|
|
Name: width .
Type: closed_interval -> int .
Computes the width of a closed interval.
|
|
Name: is_member .
Type: closed_interval -> bool .
Checks whether z belongs to i given a pair (z,i) where z is an integer and i is an interval.
|
|
Name: cons_closed_1 .
Type: int * int -> closed_interval .
Computes the interval whose lower bound is z and width is n given a pair (z,n) where z is an integer and n is a non-negative integer.
|
|
Name: are_disjoint .
Type: closed_interval * closed_interval -> bool .
Checks whether two intervals are disjoint.
|
|
Name: intersection .
Type: closed_interval * closed_interval -> closed_interval .
Computes the intersection of two non-disjoint intervals.
|
|
Name: is_subset .
Type: closed_interval * closed_interval -> bool .
Checks whether i is a proper subset of or is equal to j given a pair (i,j) of intervals (is a subset means is a proper subset of or is equal to).
|
|
Name: envelope .
Type: closed_interval * closed_interval -> closed_interval .
Computes the envelope of two intervals, that is, the smallest interval that is a superset of both.
|
|
First create a directory.
Create in this directory a file containing all these definitions and whose name has extension ml .
For instance Use_closed_interval.ml .
Write also in this directory the content of the archive Closed_interval , that is, the files Closed_interval.cmi and Closed_interval.cmo (in other words: the module Closed_interval ).
Enter this directory.
Build a Caml toplevel that contains the compiled implementation of the module Closed_interval preloaded at start-up.
To do this, execute the Unix command
Closed_interval , which allows to use short names instead of long ones in dotted notation (that is, for instance lower_bound instead of Closed_interval.lower_bound ).
Then evaluate the Caml phrase
|
![]() | ![]() | ![]() | Exercise 4 - Closed intervals whose bounds are integers | Documentation and user's manual | Table of contents | OCaml programs |