Concepts of Programming Languages

(Sean Pound) #1
15.11 A Comparison of Functional and Imperative Languages 719

This version simply specifies three steps:


  1. Build the list of numbers ([1..n]).

  2. Create a new list by mapping a function that computes the cube of a
    number onto each number in the list.

  3. Sum the new list.


Because of the lack of details of variables and iteration control, this version is
more readable than the C version.^16
Concurrent execution in the imperative languages is difficult to design and
difficult to use, as we saw in Chapter 13. In an imperative language, the pro-
grammer must make a static division of the program into its concurrent parts,
which are then written as tasks, whose execution often must be synchronized.
This can be a complicated process. Programs in functional languages are natu-
rally divided into functions. In a pure functional language, these functions are
independent in the sense that they do not create side effects and their operations
do not depend on any nonlocal or global variables. Therefore, it is much easier
to determine which of them can be concurrently executed. The actual parameter
expressions in calls often can be evaluated concurrently. Simply by specifying
that it can be done, a function can be implicitly evaluated in a separate thread,
as in Multilisp. And, of course, access to shared immutable data does not require
synchronization.
One simple factor that strongly affects the complexity of imperative, or
procedural programming, is the necessary attention of the programmer to the
state of the program at each step of its development. In a large program, the
state of the program is a large number of values (for the large number of pro-
gram variables). In pure functional programming, there is no state; hence, no
need to devote attention to keeping it in mind.
It is not a simple matter to determine precisely why functional languages
have not attained greater popularity. The inefficiency of the early implementa-
tions was clearly a factor then, and it is likely that at least some contemporary
imperative programmers still believe that programs written in functional lan-
guages are slow. In addition, the vast majority of programmers learn program-
ming using imperative languages, which makes functional programs appear to
them to be strange and difficult to understand. For many who are comfort-
able with imperative programming, the switch to functional programming is
an unattractive and potentially difficult move. On the other hand, those who
begin with a functional language never notice anything strange about func-
tional programs.


  1. Of course, the C version could have been written in a more functional style, but most C pro-
    grammers probably would not write it that way.

Free download pdf