Book contents
4 - Imperative Aspects
Published online by Cambridge University Press: 05 June 2012
Summary
All those aspects of Caml that cannot be described in a purely functional view of the language are known as its imperative qualities
either because they make sense only with respect to a particular evaluation strategy,
or because they refer to the machine representation of data structures.
Among the imperative aspects of that first kind, there are exceptions and input-output.
Among the second kind of imperative aspects, we find destructive operations such as assignment. The effect of such operations can be explained completely only by reference to formal semantics or to a description of the implementation of data structures. (We will get to those ideas later in Chapter 12.) However, we can still give you a reasonable description here based on examples.
Exceptions
In Section 2.3.4, we touched on the problem of writing partial functions. To do that, we introduced the type
This solution can hardly take into account all the situations where we need partial functions. For example, division is a partial operation (since division by 0 (zero) is not defined), but it would not be practical to replace the types int and float by the types int option and float option in every numeric calculation because doing so assumes that all arithmetic operations foresee the case where one of their arguments is undefined. The chief effect of that assumption would be to make numeric calculations impractical simply because they would be too inefficient to perform!
- Type
- Chapter
- Information
- The Functional Approach to Programming , pp. 107 - 132Publisher: Cambridge University PressPrint publication year: 1998