Cambridge University Press 1.1 Time Clock User Manual


 
10 CHAPTER 2. A TASTE OF CAML
σ
= f(σ).
2
In functional programming this view is emphasized: the program is
actually an expression that corresponds to the mathematical function f. Functional
languages support the construction of such expressions by allowing rather powerful
functional constructs.
Functional programming can be contrasted with imperative programming either
in a negative or a positive sense. Negatively, pure functional programs do not use
variables there is no state. Consequently, they cannot use assignments, since
there is nothing to assign to. Furthermore the idea of executing commands in se-
quence is meaningless, since the first command can make no difference to the second,
there being no state to mediate between them. Positively however, functional pro-
grams can use functions in much more sophisticated ways. Functions can be treated
in exactly the same way as simpler objects like integers: they can be passed to other
functions as arguments and returned as results, and in general calculated with. In-
stead of sequencing and looping, functional languages use recursive functions, i.e.
functions that are defined in terms of themselves. By contrast, most traditional lan-
guages provide poor facilities in these areas. C allows some limited manipulation of
functions via pointers, but does not allow one to create new functions dynamically.
FORTRAN does not even support recursion at all.
A potential advantage of functional languages is the following. Since the eval-
uation of expressions has no side-effect on any state, separate subexpressions can
be evaluated in any order without affecting each other. This makes programs more
comprehensible and debugging easier, since there is no danger of one part of a
program unexpectedly affecting others. Moreover, functional programs may lend
themselves well to parallel implementation, i.e. the computer can automatically
farm out different subexpressions to different processors. By contrast, imperative
programs often impose a fairly rigid order of execution, and even the limited inter-
leaving of instructions in modern pipelined processors turns out to be complicated
and full of technical problems.
Actually, CAML is not a purely functional programming language; it does have
variables and assignments if required. Most of the time, we will work inside the
purely functional subset. But even when we do use assignments, and lose some of
the preceding benefits, there are advantages in the more flexible use of functions
that languages like CAML allow. Programs can often be expressed in a very con-
cise and elegant style using higher-order functions (functions that operate on other
functions). Code can be made more general, since it can be parametrized even over
other functions. For example, a program to add up a list of numbers and a pro-
gram to multiply a list of numbers can be seen as instances of the same program,
parametrized by the pairwise arithmetic operation and the corresponding identity.
In one case it is given + and 0 and in the other case, and 1.
2.2 Basic use of CAML
We will use CAML in its interactive and interpretive mode. When it is started it
presents its prompt (‘#’):
> Caml Light version 0.74
#
(In order to exit the system, simply type ctrl/d or quit();; at the prompt.)
When CAML presents you with its prompt, you can type in expressions, terminated
2
Compare Naur’s remarks (Raphael 1966) that he can write any program in a single statement
Output = Program(Input).